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
/
signaler.js
105 lines (91 loc) · 3.46 KB
/
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// <script src="/reliable-signaler/signaler.js"></script>
function initReliableSignaler(connection, socketURL) {
var socket;
if (!connection) throw '"connection" argument is required.';
function initSocket() {
if (socket && connection && connection.isInitiator && connection.roomid) {
socket.emit('keep-session', connection.roomid);
}
socket = io.connect(socketURL || '/');
socket.on('connect', function() {
// if socket.io was disconnected out of network issues
if (socket.isHavingError) {
initSocket();
}
});
socket.on('message', function(data) {
if (onMessageCallbacks[data.channel]) {
onMessageCallbacks[data.channel](data.message);
};
});
socket.on('error', function() {
socket.isHavingError = true;
initSocket();
});
socket.on('disconnect', function() {
socket.isHavingError = true;
initSocket();
});
}
initSocket();
var onMessageCallbacks = {};
// using socket.io for signaling
connection.openSignalingChannel = function(config) {
var channel = config.channel || this.channel || 'default-channel';
onMessageCallbacks[channel] = config.onmessage;
if(config.onopen) setTimeout(config.onopen, 1);
return {
send: function(message) {
socket.emit('message', {
sender: connection.userid,
channel: channel,
message: message
});
},
channel: channel
};
};
function listenEventHandler(eventName, eventHandler) {
window.removeEventListener(eventName, eventHandler);
window.addEventListener(eventName, eventHandler, false);
}
listenEventHandler('load', onLineOffLineHandler);
listenEventHandler('online', onLineOffLineHandler);
listenEventHandler('offline', onLineOffLineHandler);
function onLineOffLineHandler() {
if (!navigator.onLine) {
console.warn('Internet channel seems disconnected.');
return;
}
// if socket.io was disconnected out of network issues
if (socket.isHavingError) {
initSocket();
}
}
function getRandomString() {
if (window.crypto && window.crypto.getRandomValues && navigator.userAgent.indexOf('Safari') === -1) {
var a = window.crypto.getRandomValues(new Uint32Array(3)),
token = '';
for (var i = 0, l = a.length; i < l; i++) {
token += a[i].toString(36);
}
return token;
} else {
return (Math.random() * new Date().getTime()).toString(36).replace(/\./g, '');
}
}
return {
socket: socket,
createNewRoomOnServer: function(roomid, successCallback) {
// for reusability on failures & reconnect
connection.roomid = roomid;
connection.isInitiator = true;
connection.userid = connection.userid || getRandomString();
socket.emit('keep-in-server', roomid || connection.channel, successCallback || function() {});
},
getRoomFromServer: function(roomid, callback) {
connection.userid = connection.userid || getRandomString();
socket.emit('get-session-info', roomid, callback);
}
};
}