-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (36 loc) · 1.23 KB
/
index.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
// iMotions UDP API config:
var PORT = 8089;
var HOST = '127.0.0.1';
// HTTP port:
var HTTPPORT = 1337;
var dgram = require('dgram');
var http = require('http');
var WebSocketServer = require('websocket').server;
var iMotionsClient = dgram.createSocket('udp4');
var server = http.createServer(function(request, response) {
// just for handling websockets
});
server.listen(1337, function() { });
// create the websocket server
wsServer = new WebSocketServer({
httpServer: server
});
// WebSocket event handling
wsServer.on('request', function(request) {
var connection = request.accept(null, request.origin);
connection.on('message', function(message) {
if (message.type === 'utf8') {
console.log("Received from webclient: "+message.utf8Data);
// create bytes object and send to iMotions
buffer = Buffer(message.utf8Data,"utf8");
iMotionsClient.send(buffer, PORT, HOST, function(err, bytes) {
if (err) throw err;
console.log('Bridged to iMotions: ' + HOST +':'+ PORT);
});
}else{
console.log("Warning, received non-utf8 data. Messages have to be utf8 encoded.");
}
});
connection.on('close', function(connection) {
});
});