-
Notifications
You must be signed in to change notification settings - Fork 28
/
hexapod_srv.js
59 lines (45 loc) · 1.29 KB
/
hexapod_srv.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
var hexapod = require('./hexapod_core.js')
hexapod.init()
var command
var timeout_id
var WebSocketServer = require('ws').Server
, wss = new WebSocketServer({port: 8001});
var control_ws = null;
wss.on('error', function(err) {
console.log("Error: %s", err);
});
wss.on('connection', function connection(ws) {
if(control_ws)
{
// read only mode
ws.on('close', function (code, message) {
console.log("[WSS] Viewer is gone: %s, %s", code, message);
});
ws.send('reject');
console.log("[WSS] Viewer joined: %s", ws);
}
else {
control_ws = ws;
ws.on('close', function (code, message) {
control_ws = null;
console.log("[WSS] Controller is gone: %s, %s", code, message);
});
ws.on('message', function incoming(message) {
console.log('Command received: %s', message);
if(command != message) {
command = message
hexapod.command(command)
}
// reset timer whenver new command receive
clearTimeout(timeout_id)
timeout_id = setTimeout(function() {
command = hexapod.mode.modeStop
console.log("Timeout! stop")
hexapod.command(command)
}, 5000)
});
ws.send('control mode');
console.log("Controller joined: %s", ws);
}
});
console.log("Server started...")