This repository has been archived by the owner on Sep 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.js
51 lines (47 loc) · 1.66 KB
/
server.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
const WebSocket = require('ws');
const url = require('url');
const server = new WebSocket.Server({
// host: process.argv[1] || '0.0.0.0',
port: process.argv[2] || 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 STATE = {};
server.on('connection', function connection(ws, req) {
const ip = req.connection.remoteAddress;
const port = req.connection.remotePort;
const pathname = url.parse(req.url).pathname;
console.log(ip, port, pathname, 'connected');
if (!STATE[pathname]) {
STATE[pathname] = {};
}
ws.send(JSON.stringify(STATE[pathname]));
ws.on('message', function incoming(message) {
console.log(ip, port, message)
const data = JSON.parse(message);
Object.assign(STATE[pathname], data);
// Broadcast to everyone else.
server.clients.forEach(function each(client) {
if (client !== ws && client.readyState === WebSocket.OPEN) {
client.send(message);
}
});
});
});