-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebsocketManager.js
58 lines (49 loc) · 1.63 KB
/
WebsocketManager.js
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
'use strict';
const http = require('http');
const WebsocketClient = require('./WebsocketClient');
const WebsocketServer = require('websocket').server;
const EventEmitter = require('events');
class WebsocketManager extends EventEmitter {
constructor() {
super();
this.clients = [];
}
start(httpServer) {
this.websocketServer = new WebsocketServer({ httpServer: httpServer, autoAcceptConnections: false, path: '/socket' });
this.websocketServer.on('request', (request) => {
this.onWebsocketConnection(request);
});
}
onWebsocketConnection(request) {
console.log(`${new Date()} Websocket connection accepted.`);
let connection = request.accept(null, request.origin);
let client = new WebsocketClient(connection);
this.clients.push(client);
connection.on('message', (message) => {
if (message.type === 'utf8') {
console.log(`${new Date()} Websocket server received message: ${message.utf8Data}`);
let data = JSON.parse(message.utf8Data);
this.onMessageReceivedFromClient(client, data);
}
});
connection.on('close', () => {
let index = this.clients.indexOf(client);
if (index >= 0) {
this.clients.splice(index, 1);
console.log(`${new Date()} Websocket client ${connection.remoteAddress} disconnected.`);
}
});
}
onMessageReceivedFromClient(client, message) {
this.emit('message', client, message);
}
sendMessageToClient(client, message) {
client.send(message);
}
sendMessageToClients(message) {
for (let client of this.clients) {
client.send(message);
}
}
}
module.exports = WebsocketManager;