-
Notifications
You must be signed in to change notification settings - Fork 17
/
dispatcher.js
40 lines (33 loc) · 833 Bytes
/
dispatcher.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
var async = require('async');
var clients = [];
var data_key;
// init websocket listener
exports.init = function(httpd, key) {
var io = require('socket.io').listen(httpd);
io.set('log level', 1);
data_key = key;
io.sockets.on('connection', function(socket) {
clients.push(socket);
console.log( "Client connected: " + socket );
});
io.sockets.on('disconnect', function(socket) {
clients.pull( clients.indexOf(socket) );
console.log( "Client disconnected: " + socket );
});
};
// emit a single JSON document to all clients
exports.emit = function(data) {
if ( clients.length > 0 ) {
console.info("Starting push to clients ...");
async.eachSeries(
clients,
function(socket, callback) {
socket.emit(data_key, data);
callback();
},
function(err) {
console.info(err);
}
);
}
};