diff options
author | Joachim Bauch <bauch@struktur.de> | 2017-08-31 18:06:29 +0300 |
---|---|---|
committer | Joachim Bauch <bauch@struktur.de> | 2017-11-02 13:23:11 +0300 |
commit | e3139fe172255a7e388a8b5f9f7ee6c09090a311 (patch) | |
tree | 8e89cfa699f12340a6cbd81c20c9d64ad2014a9b /js/signaling.js | |
parent | 51ba92cb032901a646228439561a5a6cb6499f7e (diff) |
Detect and handle case where sessions no longer exist after a reconnect.
Signed-off-by: Joachim Bauch <bauch@struktur.de>
Diffstat (limited to 'js/signaling.js')
-rw-r--r-- | js/signaling.js | 36 |
1 files changed, 32 insertions, 4 deletions
diff --git a/js/signaling.js b/js/signaling.js index e5cc7a3b0..454992527 100644 --- a/js/signaling.js +++ b/js/signaling.js @@ -462,6 +462,7 @@ this.initialReconnectIntervalMs = 1000; this.maxReconnectIntervalMs = 16000; this.reconnectIntervalMs = this.initialReconnectIntervalMs; + this.joinedUsers = {}; this.connect(); } @@ -477,6 +478,7 @@ // simultaneously in case the server connection is interrupted. var interval = this.reconnectIntervalMs - (this.reconnectIntervalMs / 2) + (this.reconnectIntervalMs * Math.random()); console.log("Reconnect in", interval); + this.reconnected = true; this.reconnectTimer = window.setTimeout(function() { this.reconnectTimer = null; this.connect(); @@ -708,6 +710,7 @@ } }, function(data) { console.log("Left", data); + this.joinedUsers = {}; this.currentCallToken = null; }.bind(this)); }; @@ -727,14 +730,39 @@ }; StandaloneSignaling.prototype.processRoomEvent = function(data) { + var i; switch (data.event.type) { case "join": - console.log("Users joined", data.event.join); - this._trigger("usersJoined", [data.event.join]); + var joinedUsers = data.event.join || []; + if (joinedUsers.length) { + console.log("Users joined", joinedUsers); + var leftUsers = {}; + if (this.reconnected) { + this.reconnected = false; + // The browser reconnected, some of the previous sessions + // may now no longer exist. + leftUsers = _.extend({}, this.joinedUsers); + } + for (i = 0; i < joinedUsers.length; i++) { + this.joinedUsers[joinedUsers[i].sessionid] = true; + delete leftUsers[joinedUsers[i].sessionid]; + } + leftUsers = _.keys(leftUsers); + if (leftUsers.length) { + this._trigger("usersLeft", [leftUsers]); + } + this._trigger("usersJoined", [joinedUsers]); + } break; case "leave": - console.log("Users left", data.event.leave); - this._trigger("usersLeft", [data.event.leave]); + var leftSessionIds = data.event.leave || []; + if (leftSessionIds.length) { + console.log("Users left", leftSessionIds); + for (i = 0; i < leftSessionIds.length; i++) { + delete this.joinedUsers[leftSessionIds[i]]; + } + this._trigger("usersLeft", [leftSessionIds]); + } break; default: console.log("Unknown room event", data); |