-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
62 lines (51 loc) · 1.77 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
61
62
const express = require('express');
const next = require('next');
const http = require('http');
const socket = require('socket.io');
let controls = require('./config').map(control => ({ ...control, value: control.min }));
const port = parseInt(process.env.PORT, 10) || 3000;
const nextApp = next({ dev: process.env.NODE_ENV !== 'production' });
const nextRequestHandler = nextApp.getRequestHandler();
const updateValue = (control, type) => {
const operations = {
'+': v => v + control.increment,
'-': v => v - control.increment,
default: v => v,
};
const newValue = (operations[type] || operations.default)(control.value);
return newValue >= control.min && newValue <= control.max
? newValue
: control.value;
};
const handleSender = ({ control: controlIndex, type }) => controls.map((currentControl, index) =>
index === controlIndex
? { ...currentControl, value: updateValue(currentControl, type) }
: currentControl
);
let latestUpdate = new Date();
nextApp.prepare().then(() => {
const app = express();
const httpServer = http.Server(app);
const io = socket(httpServer);
app.get('*', (req, res) => {
return nextRequestHandler(req, res);
});
io.on('connection', function (socket) {
console.log('a user connected');
socket.emit('controls', controls);
socket.on('sender', function (msg) {
console.log('message:', msg);
controls = handleSender(msg);
socket.emit('controls', controls);
const now = new Date();
if(now - latestUpdate >= 200) {
socket.broadcast.emit('controls', controls);
latestUpdate = now;
}
});
});
httpServer.listen(port, '0.0.0.0', err => {
if (err) { throw err }
console.log(`> Ready on http://localhost:${port}`);
});
}).catch(console.error);