-
Notifications
You must be signed in to change notification settings - Fork 7
/
server-ws.cjs
83 lines (75 loc) · 2.43 KB
/
server-ws.cjs
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
78
79
80
81
82
83
const WebSocket = require('ws');
const url = require('url');
const server = new WebSocket.Server({
host: process.env.HOST || '0.0.0.0',
port: process.env.PORT || 8089,
perMessageDeflate: {
zlibDeflateOptions: {
// See zlib defaults.
chunkSize: 1024,
memLevel: 7,
level: 3
},
zlibInflateOptions: {
chunkSize: 10 * 1024
},
// Other options settable:
clientNoContextTakeover: true, // Defaults to negotiated value.
serverNoContextTakeover: true, // Defaults to negotiated value.
serverMaxWindowBits: 10, // Defaults to negotiated value.
// Below options specified as default values.
concurrencyLimit: 10, // Limits zlib concurrency for perf.
threshold: 1024 // Size (in bytes) below which messages
// should not be compressed.
}
});
let STATES = {};
let CLIENTS = {};
server.on('connection', function connection(ws, req) {
const ip = req.socket.remoteAddress;
const port = req.socket.remotePort;
const pathname = url.parse(req.url).pathname;
console.log(ip, port, pathname, 'connected', STATES[pathname]);
if (!STATES[pathname]) {
STATES[pathname] = {};
}
if (!CLIENTS[pathname]) {
CLIENTS[pathname] = new Set();
}
CLIENTS[pathname].add(ws);
ws.send(JSON.stringify(STATES[pathname]));
ws.on('message', function incoming(message) {
message = message.toString()
// console.log(ip, port, message)
const data = JSON.parse(message);
Object.assign(STATES[pathname], data);
// Broadcast to everyone else.
CLIENTS[pathname].forEach(function each(client) {
if (client !== ws && client.readyState === WebSocket.OPEN) {
client.send(message);
}
});
});
ws.isAlive = true;
ws.on('error', console.error);
ws.on('pong', heartbeat);
ws.on('close', function() {
CLIENTS[pathname].delete(ws);
if (CLIENTS[pathname].size == 0) {
CLIENTS[pathname] = undefined;
}
});
});
function heartbeat() {
this.isAlive = true;
}
const interval = setInterval(function ping() {
server.clients.forEach(function each(ws) {
if (server.isAlive === false) return ws.terminate();
ws.isAlive = false;
ws.ping();
});
}, 30000);
server.on('close', function close() {
clearInterval(interval);
});