-
Notifications
You must be signed in to change notification settings - Fork 1
/
UDPServer.js
48 lines (38 loc) · 1.07 KB
/
UDPServer.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
const osc = require('osc');
const EventEmitter = require('events');
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();
const engine = require('engine.io');
const server = engine.listen(1337);
// Create an osc.js UDP Port listening on port 57121.
var udpPort = new osc.UDPPort({
localAddress: "0.0.0.0",
localPort: 57121
});
// UDP connection to MAX
udpPort.open();
// Positional level bundles
udpPort.on("bundle", function (data, timeTag, info) {
let message = {
type: 'position'
};
Object.assign(message, data);
myEmitter.emit('bundle', message);
});
// Channel level message
udpPort.on("message", function (data, timeTag, info) {
let message = {
type: 'channel'
};
Object.assign(message, data);
myEmitter.emit('message', message);
});
// Browser Web Socket connection
server.on('connection', function(socket){
myEmitter.on('bundle', function(data) {
socket.send(JSON.stringify(data));
})
myEmitter.on('message', function (data) {
socket.send(JSON.stringify(data));
})
})