-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMessageHandler.js
More file actions
48 lines (47 loc) · 1.55 KB
/
MessageHandler.js
File metadata and controls
48 lines (47 loc) · 1.55 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
class MessageHandler {
constructor(server, ws, request, WebSocket) {
this.server = server
this.ws = ws
this.secWebsocketKey = request.headers['sec-websocket-key']
this.server.users = this.server.users || {}
this.server.users[this.secWebsocketKey] = this.ws
this.WebSocket = WebSocket
this.rooms = {}
this.roomList = null
}
user(secWebsocketKey, eventName, data) {
let info = { eventName, data }
this.server.users[secWebsocketKey].send(JSON.stringify(info));
}
me(eventName, data) {
let info = { eventName, data }
this.server.users[this.secWebsocketKey].send(JSON.stringify(info));
}
all(eventName, data) {
let info = { eventName, data }
this.server.clients.forEach((client) => {
if (client.readyState === this.WebSocket.OPEN) {
client.send(JSON.stringify(info));
}
});
}
others(eventName, data) {
let info = { eventName, data }
this.server.clients.forEach((client) => {
if (client !== this.ws && client.readyState === this.WebSocket.OPEN) {
client.send(JSON.stringify(info));
}
});
}
setRoomList(roomList){
this.roomList = roomList
}
room(listId, eventName, data) {
if(this.roomList == null) return
let info = { eventName, data }
this.roomList[listId].map(key => {
this.server.users[key].send(JSON.stringify(info));
})
}
}
module.exports = MessageHandler