forked from haystack/nb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocketapi.js
More file actions
77 lines (68 loc) · 2.77 KB
/
Copy pathsocketapi.js
File metadata and controls
77 lines (68 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const io = require( "socket.io" )();
const socketapi = {
io: io
};
let global_section_connections = {}
let section_connections = {}
let dict = {}
// Add your socket.io logic here!
io.on('connection', function( socket ) {
// socket.emit('id', socket.id) // send each client their socket id
socket.on('joined', function(data) {
let username = data.username
let classId = data.classId
let sectionId = data.sectionId
if (sectionId && sectionId.length > 0) { // use the section_connections and sectionId
if (!(sectionId in section_connections)) {
section_connections[sectionId] = []
}
section_connections[sectionId].push(username)
io.emit('connections', {classId: classId, sectionId: sectionId, connections: section_connections[sectionId]})
} else if (classId && classId.length >0) { // use the global_section_connections and classId
if (!(classId in global_section_connections)) {
global_section_connections[classId] = []
}
global_section_connections[classId].push(username)
io.emit('connections', {classId: classId, sectionId: sectionId, connections: global_section_connections[classId]})
}
})
socket.on('left', function(data) {
let username = data.username
let classId = data.classId
let sectionId = data.sectionId
if (sectionId && sectionId.length > 0) { // use the section_connections and sectionId
if (sectionId in section_connections) {
let idx = section_connections[sectionId].indexOf(username)
if (idx >= 0) {
section_connections[sectionId].splice(idx, 1)
io.emit('connections', {classId: classId, sectionId: sectionId, connections: section_connections[sectionId]})
}
}
} else if (classId && classId.length > 0){ // use the global_section_connections and classId
if (classId in global_section_connections) {
let idx = global_section_connections[classId].indexOf(username)
if (idx >= 0) {
global_section_connections[classId].splice(idx, 1)
io.emit('connections', {classId: classId, sectionId: sectionId, connections: section_connections[classId]})
}
}
}
})
socket.on('thread-typing', function(data) {
if (data.threadId in dict) {
dict[data.threadId].add(data.username)
} else {
dict[data.threadId] = new Set([data.username])
}
io.emit('thread-typing', {usersTyping: [...dict[data.threadId]], threadId: data.threadId})
})
socket.on('thread-stop-typing', function(data) {
if (data.threadId in dict) {
dict[data.threadId].delete(data.username)
if (dict[data.threadId].size == 0) {
io.emit('thread-typing', {usersTyping: [...dict[data.threadId]], threadId: data.threadId})
}
}
})
});
module.exports = socketapi;