This repository has been archived by the owner on Apr 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
reliable-signaler.js
58 lines (45 loc) · 1.6 KB
/
reliable-signaler.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
// This file handles socket.io based reliable signaling
// require('reliable-signaler')(httpServer || expressServer || portNumber);
exports.ReliableSignaler = ReliableSignaler;
function ReliableSignaler(app, socketCallback) {
var io = require('socket.io').listen(app, {
log: false,
origins: '*:*'
});
var listOfRooms = {};
io.on('connection', function(socket) {
var currentUser = socket;
socket.on('keep-in-server', function(roomid, callback) {
listOfRooms[roomid] = roomid;
currentUser.roomid = roomid;
if(callback) callback();
});
socket.on('get-session-info', function(roomid, callback) {
if (!!listOfRooms[roomid]) {
callback(listOfRooms[roomid]);
return;
}
(function recursive() {
if (currentUser && listOfRooms[roomid]) {
callback(listOfRooms[roomid]);
return
}
setTimeout(recursive, 1000);
})();
});
socket.on('message', function(message) {
socket.broadcast.emit('message', message);
});
socket.on('disconnect', function() {
if (!currentUser) return;
// autoCloseEntireSession = true;
if (currentUser && currentUser.roomid && listOfRooms[currentUser.roomid]) {
delete listOfRooms[currentUser.roomid];
}
currentUser = null;
});
if(socketCallback) {
socketCallback(socket);
}
});
}