-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathserver.js
More file actions
39 lines (33 loc) · 911 Bytes
/
server.js
File metadata and controls
39 lines (33 loc) · 911 Bytes
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
var WebSocketServer = require('ws').Server;
var mapleTree = require('mapleTree');
var port = process.env.ZUUL_PORT || process.env.PORT || 3000;
module.exports = function () {
var router = new mapleTree.RouteTree();
var wss = new WebSocketServer({ port: port });
router.define('/read', function(ws) {
var values = ['a', 'b', 'c', 'd'];
var timer = setInterval(function() {
var next = values.shift();
if (next) {
ws.send(next);
}
else {
clearInterval(timer);
ws.close();
}
}, 100);
});
router.define('/echo', function(ws) {
ws.on('message', function(data) {
console.log('received message: ', data);
ws.send(data);
});
});
wss.on('connection', function(ws, request) {
var match = router.match(request.url);
if (match && typeof match.fn == 'function') {
match.fn(ws);
}
});
return wss
}