forked from mimeindustries/mearm-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·47 lines (40 loc) · 1.31 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
var HttpServer = require('./lib/HttpServer.js').HttpServer,
WebSocket = require('ws'),
WsServer = WebSocket.Server,
MiroComms = require('./lib/MiroComms.js').MiroComms;
try{
var MeArmPi = require('./lib/MeArmPi.js').MeArmPi,
PiJoysticks = require('./lib/PiJoysticks.js').PiJoysticks,
arm = new MeArmPi(),
joysticks = new PiJoysticks(arm),
statusLED = require('./lib/StatusLED');
statusLED.start(5000);
console.log("Booting on Raspberry Pi");
}catch(e){
console.log(e)
var DummyArm = require('./lib/DummyArm.js').DummyArm,
arm = new DummyArm();
console.log("Booting with dummy arm");
}
var comms = new MiroComms(arm);
if(process.argv.indexOf('--no-ui') < 0){
// Set up the http server for serving out our static UI
var port = process.argv.length >= 3 ? process.argv[2] : 80
var httpd = new HttpServer('./web-ui', port);
}
// Set up the WebSocket server
var s = new WsServer({port: 8899});
s.on('connection', function(ws) {
console.log("New WebSocket Connection");
ws.on('message', function(data, flags) {
msg = JSON.parse(data)
comms.handle_msg(msg);
});
});
comms.on('msg', function(msg){
s.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify(msg));
}
});
});