Skip to content

Instantly share code, notes, and snippets.

@lminiero
Created April 12, 2018 14:11
Show Gist options
  • Save lminiero/54020fbb3be7336292cf36ece2152b85 to your computer and use it in GitHub Desktop.
Save lminiero/54020fbb3be7336292cf36ece2152b85 to your computer and use it in GitHub Desktop.
diff --git a/html/janus.js b/html/janus.js
index e7bed83..ad8584a 100644
--- a/html/janus.js
+++ b/html/janus.js
@@ -393,6 +393,13 @@ function Janus(gatewayCallbacks) {
// Public methods
this.getServer = function() { return server; };
this.isConnected = function() { return connected; };
+ this.reconnect = function(callbacks) {
+ callbacks = callbacks || {};
+ callbacks.success = (typeof callbacks.success == "function") ? callbacks.success : Janus.noop;
+ callbacks.error = (typeof callbacks.error == "function") ? callbacks.error : Janus.noop;
+ callbacks["reconnect"] = true;
+ createSession(callbacks);
+ };
this.getSessionId = function() { return sessionId; };
this.destroy = function(callbacks) { destroySession(callbacks); };
this.attach = function(callbacks) { createHandle(callbacks); };
@@ -657,6 +664,22 @@ function Janus(gatewayCallbacks) {
function createSession(callbacks) {
var transaction = Janus.randomString(12);
var request = { "janus": "create", "transaction": transaction };
+ if(callbacks["reconnect"]) {
+ // We're reconnecting, claim the session
+ connected = false;
+ request["janus"] = "claim";
+ request["session_id"] = sessionId;
+ // If we were using websockets, ignore the old connection
+ if(ws) {
+ ws.onopen = null;
+ ws.onerror = null;
+ ws.onclose = null;
+ if(wsKeepaliveTimeoutId) {
+ clearTimeout(wsKeepaliveTimeoutId);
+ wsKeepaliveTimeoutId = null;
+ }
+ }
+ }
if(token !== null && token !== undefined)
request["token"] = token;
if(apisecret !== null && apisecret !== undefined)
@@ -705,8 +728,12 @@ function Janus(gatewayCallbacks) {
}
wsKeepaliveTimeoutId = setTimeout(keepAlive, 30000);
connected = true;
- sessionId = json.data["id"];
- Janus.log("Created session: " + sessionId);
+ sessionId = json["session_id"] ? json["session_id"] : json.data["id"];
+ if(callbacks["reconnect"]) {
+ Janus.log("Claimed session: " + sessionId);
+ } else {
+ Janus.log("Created session: " + sessionId);
+ }
Janus.sessions[sessionId] = that;
callbacks.success();
};
@@ -745,8 +772,12 @@ function Janus(gatewayCallbacks) {
return;
}
connected = true;
- sessionId = json.data["id"];
- Janus.log("Created session: " + sessionId);
+ sessionId = json["session_id"] ? json["session_id"] : json.data["id"];
+ if(callbacks["reconnect"]) {
+ Janus.log("Claimed session: " + sessionId);
+ } else {
+ Janus.log("Created session: " + sessionId);
+ }
Janus.sessions[sessionId] = that;
eventHandler();
callbacks.success();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment