-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket.js
70 lines (62 loc) · 2.1 KB
/
websocket.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
module.exports = function(httpServer) {
var WebSocketServer = require('websocket').server;
wsServer = new WebSocketServer({
httpServer: httpServer,
options: {
secure: true
}
});
wsServer.on('request', function(request) {
var connection = request.accept('p1', request.origin);
console.log((new Date()) + ' Connection accepted.');
connection.on('message', function(message) {
if (message.type === 'utf8') {
var data;
try {
data = JSON.parse(message.utf8Data);
if (data.dateRange) {
data.dateRange = {
from: new Date(data.dateRange.from),
to: new Date(data.dateRange.to)
}
}
} catch (e) {
console.log('ERROR parsing data from client', e);
}
if (data) {
posUpdate(data.pos, data.dateRange, connection);
}
} else {
console.log('ERROR Received bad message type');
}
});
connection.on('close', function(reasonCode, description) {
console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
});
});
var posUpdateCallback = function(pos, fn) {}
function posUpdate(pos, dateRange, con) {
if (pos.initialPos) {
posUpdateCallback(pos.initialPos, dateRange, sendEvents);
} else if (pos.newPos) {
posUpdateCallback(pos.newPos, dateRange, sendEvents);
} else {
console.log('ERROR Invalid object received', pos);
}
function sendEvents(events) {
con.send(JSON.stringify(events));
}
}
return {
onNewPos: function(fn) {
posUpdateCallback = fn;
}
}
};
if (!module.parent) {
console.log('Starting as standalone');
var t = module.exports();
t.onNewPos(function(pos, fn) {
console.log(pos, fn(pos));
});
}