/
app.js
75 lines (66 loc) · 2.47 KB
/
app.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const WebSocketServer = require('ws').Server;
const https = require('https');
const fs = require('fs');
const privateKey = fs.readFileSync('cert/cert.key', 'utf8');
const certificate = fs.readFileSync('cert/cert.pem', 'utf8');
const credentials = { key: privateKey, cert: certificate };
const httpsServer = https.createServer(credentials);
const port = 1338;
const wss = new WebSocketServer({
server: httpsServer
});
httpsServer.listen(port);
const chambers = {};
// Creating connection using websocket
wss.on('connection', ws => {
console.log('new client connected');
// sending message
ws.on('message', (data, isBinary) => {
console.log(`Client has sent us: ${data}`)
const jsonData = JSON.parse(data);
const chamberName = jsonData.chamber;
switch (jsonData.command) {
case 'join':
console.log('case join');
if (chamberName in chambers) {
chambers[chamberName].mages.push(ws);
const broadcastData = {
command: 'update',
flowchart: chambers[chamberName].flowchart
}
ws.send(JSON.stringify(broadcastData), { binary: isBinary });
}
else {
chambers[chamberName] = {
flowchart: jsonData.flowchart,
owner: ws,
mages: [ws]
}
}
break;
case 'update':
console.log('case update');
chambers[chamberName].flowchart = jsonData.flowchart;
const broadcastData = {
command: 'update',
flowchart: jsonData.flowchart
}
chambers[chamberName].mages.forEach(mage => {
if (mage !== ws && mage.readyState == 1) {
console.log('Sending update info to ', mage);
mage.send(JSON.stringify(broadcastData), { binary: isBinary });
}
});
break;
}
});
// handling what to do when clients disconnects from server
ws.on('close', () => {
console.log('the client has connected');
});
// handling client connection error
ws.onerror = function () {
console.log('Some Error occurred')
}
});
console.log(`The WebSocket server is running on port ${port}`);