-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsocketio.js
81 lines (58 loc) · 1.61 KB
/
socketio.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
var http = require('http'),
socketio = require('socket.io'),
fs = require('fs'),
osc = require('osc-min'),
dgram = require('dgram'),
remote_osc_ip;
var http_server = http.createServer(function(req, res) {
fs.readFile(__dirname + '/socketio.html', function(err, data) {
if(err) {
res.writeHead(500);
return res.end('Error loading socket.io.html');
}
res.writeHead(200);
res.end(data);
});
});
var io = socketio(http_server);
var udp_server = dgram.createSocket('udp4', function(msg, rinfo) {
var osc_message;
try {
osc_message = osc.fromBuffer(msg);
} catch(err) {
return console.log('Could not decode OSC message');
}
if(osc_message.address != '/socketio') {
return console.log('Invalid OSC address');
}
remote_osc_ip = rinfo.address;
io.emit('osc', {
x: parseInt(osc_message.args[0].value) || 0,
y: parseInt(osc_message.args[1].value) || 0
});
});
io.on('connection', function(socket) {
socket.on('browser', function(data) {
if(! remote_osc_ip) {
return;
}
var osc_msg = osc.toBuffer({
oscType: 'message',
address: '/socketio',
args:[{
type: 'integer',
value: parseInt(data.x) || 0
},
{
type: 'integer',
value: parseInt(data.y) || 0
}]
});
udp_server.send(osc_msg, 0, osc_msg.length, 9999, remote_osc_ip);
console.log('Sent OSC message to %s:9999', remote_osc_ip);
});
});
http_server.listen(8080);
console.log('Starting HTTP server on TCP port 8080');
udp_server.bind(9998);
console.log('Starting UDP server on UDP port 9998');