-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
84 lines (63 loc) · 2.06 KB
/
Copy pathserver.js
File metadata and controls
84 lines (63 loc) · 2.06 KB
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
84
var express = require('express')
, app = express()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server);
app.use(express.static(__dirname + '/../epileptic-frontend/'));
app.use(express.json()); // to support JSON-encoded bodies
app.use(express.urlencoded()); // to support URL-encoded bodies
server.listen(7000,"0.0.0.0");
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
io.sockets.on('connection', function (socket) {
console.log('[' + socket.handshake.address.address + '] user connected');
socket.on('ping', function (clienttime, fn) {
// console.log(clienttime);
//respond:
fn({
clienttime: clienttime,
servertime: Date.now()/1000
});
});
socket.on("getRealServertime", function (traveltime, fn) {
var now = Date.now()/1000;
console.log('[' + socket.handshake.address.address + '] traveltime: ' + traveltime);
fn({
realServertime: now + traveltime,
servertime: now
});
// store traveltime inside socket :-)
socket.traveltime = traveltime;
});
socket.on('input', function(data) {
console.log(data.bpm)
console.log('[' + socket.handshake.address.address + '] input', data);
sendToClients(data.color,data.bpm);
});
socket.on('disconnect', function() {
console.log('[' + socket.handshake.address.address + '] user disconnected');
});
});
app.post('/start', function (req, res) {
var color = req.body.color,
bpm = req.body.bpm;
sendToClients(color, bpm);
res.send(200);
});
app.post('/stop', function (req, res) {
sendToClients("#111111",20);
res.send(200);
});
function sendToClients(color, bpm) {
var bpsecond = Math.round(bpm/60);
var interval = 0;
if (bpm != 0) {
interval = (1000/bpsecond);
}
var duration = (interval/5);
io.sockets.emit('data', {
color: color,
duration: duration,
interval: interval
});
}