-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
60 lines (49 loc) · 1.69 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
52
53
54
55
56
57
58
59
60
const express = require('express'),
http = require('http'),
app = express(),
server = http.createServer(app),
path = require('path'),
WebSocket = require('ws');
const wss = new WebSocket.Server({ server, path: '/ws' });
const clients = {}; // Record<UUID, WebSocket>
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
// console.log('received: %s', message);
// When a new user joins, pick another user to sync data over
const payload = JSON.parse(message);
// console.log(payload);
if (payload.event === 'join') {
// store client by client generated ID
clients[payload.id] = ws;
// use array.some like a forEach with break
for (var i = 0; i < wss.clients.length; i++) {
const client = wss.clients[i];
if (client !== ws && client.readyState === WebSocket.OPEN) {
client.send(message);
// console.log('SYNCER selected', client);
break;
}
}
}
// The user that receives 'join' payload will sync back data to new user
if (payload.event === 'sync') {
// console.log('SYNC', payload);
clients[payload.id].send(message);
}
wss.clients.forEach(function each(client) {
// dont send to same client
if (client !== ws && client.readyState === WebSocket.OPEN) {
client.send(message);
}
});
});
});
app.use(express.static(path.join(__dirname, 'dist')));
app.get('/', (req, res) => {
// res.json({'hi': 123})
res.sendFile(path.join(__dirname, 'dist', 'index.html'));
});
const port = process.env.NODE_ENV === 'production' ? 3000 : 3001;
server.listen(port, () => {
console.log(`Listening on port ${port}`);
});