forked from evanw/node-spacemouse
-
Notifications
You must be signed in to change notification settings - Fork 1
/
daemon.js
executable file
·42 lines (36 loc) · 1.12 KB
/
daemon.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
#!/usr/bin/env node
var spacemouse = require('./spacemouse');
var ws = require('ws');
function serve(options) {
options = options || {};
var port = options.port || 8123;
var rate = options.rate || 1 / 10;
var server = new ws.Server({ port: port });
var updates = spacemouse.listen({ rate: rate });
var clients = [];
updates.on('connect', function() {
console.log('[SpaceMouse] connected to device');
});
updates.on('disconnect', function() {
console.log('[SpaceMouse] disconnected from device');
});
updates.on('update', function(values) {
var message = JSON.stringify(values);
clients.map(function(client) {
client.send(message);
});
});
server.on('connection', function connection(client) {
console.log('[SpaceMouse] connected to socket');
client.on('close', function() {
console.log('[SpaceMouse] disconnected from socket');
clients.splice(clients.indexOf(client), 1);
});
clients.push(client);
});
console.log('[SpaceMouse] server listening at http://localhost:' + port + '/');
}
exports.serve = serve;
if (require.main === module) {
serve(8123);
}