forked from dktr0/extramuros
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlinkserver.js
60 lines (48 loc) · 1.56 KB
/
linkserver.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
const express = require('express');
const WebSocket = require('ws');
const http = require('http');
var nopt = require('nopt');
var osc = require('osc');
const stderr = process.stderr;
const knownOpts = {
"ws-port" : [Number, null],
"osc-send-port" : [Number, null],
"help": Boolean
};
const shortHands = {
"w" : ["--ws-port"],
"s" : ["--osc-send-port"]
};
const parsed = nopt(knownOpts,shortHands,process.argv,2);
var httpServer = http.createServer();
var expressServer = express();
expressServer.use(express.static(__dirname));
httpServer.on('request',expressServer);
var wsPort = parsed['ws-port'];
if(wsPort==null) wsPort = 8001;
//setup abletonlink-addon
const AbletonLink = require("abletonlink-addon");
const link = new AbletonLink();
var wss = new WebSocket.Server({server: httpServer});
wss.broadcast = function(data) {
for (let i of wss.clients) i.send(data);
};
setInterval(function() {
try {wss.broadcast(JSON.stringify({'type': "linkBeat", 'beat': link.getBeat(), 'phase': link.getPhase() }));}
catch(e) { stderr.write("warning: exception in WebSocket send\n"); }
}, 2 );
setInterval(function() {
//io.sockets.send('message');
try {
wss.broadcast(JSON.stringify({
'type': "linkInfos",
'numPeers': link.getNumPeers(),
'quantum' : link.getQuantum(),
'tempo' : link.getTempo()
}));
}
catch(e) { stderr.write("warning: exception in WebSocket send\n"); }
}, 3000 );
httpServer.listen(wsPort, () => {
console.log('Link server is listening on port ' + wsPort);
});