-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket_manager.js
66 lines (49 loc) · 1.56 KB
/
socket_manager.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
var SocketManager = function(io) {
this.io = io;
this.sockets = {};
this.queues = {};
var manager = this;
io.sockets.on('connection', function(socket) {
// when a socket sends an auth message, associate it with
// that socket id
socket.on('auth', function(socket_id) {
manager.sockets[socket_id] = socket;
socket.set('socket_id', socket_id);
});
// clean up
socket.on('disconnect', function() {
socket.get('socket_id', function(err, socket_id) {
delete manager.sockets[socket_id];
delete manager.queues[socket_id];
});
});
});
// send a message to a socket
this.send = function(socket_id, topic, message) {
// build a queue if it doesn't exist
if (! manager.queues[socket_id]) {
manager.queues[socket_id] = [];
}
// add this message to the socket's queue
manager.queues[socket_id].push([ topic, message ]);
};
// attempt to send all queued messages to the available sockets
this.flush = function() {
for (var socket_id in manager.queues) {
// if a socket exists for this socket_id
if (manager.sockets[socket_id]) {
// send all outstanding messages to the socket
manager.queues[socket_id].forEach(function(item) {
manager.sockets[socket_id].emit(item[0], item[1]);
});
// clear the queue
manager.queues[socket_id] = [];
}
};
};
// attempt to flush the socket queues every 1000ms
setInterval(this.flush, 1000);
};
module.exports.create = function(io) {
return new SocketManager(io);
}