From b51d935cd17144ace940ecd2a64809e0dd9c1362 Mon Sep 17 00:00:00 2001 From: Lorenzo Miniero Date: Fri, 10 Jan 2020 11:38:57 +0100 Subject: [PATCH] Use sendBeacon instead of sync XHR in onbeforeunload (fixes #1902) (#1918) --- html/janus.js | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/html/janus.js b/html/janus.js index bf58e89512..67d5e6f101 100644 --- a/html/janus.js +++ b/html/janus.js @@ -327,7 +327,7 @@ Janus.init = function(options) { for(var s in Janus.sessions) { if(Janus.sessions[s] && Janus.sessions[s].destroyOnUnload) { Janus.log("Destroying session " + s); - Janus.sessions[s].destroy({asyncRequest: false, notifyDestroyed: false}); + Janus.sessions[s].destroy({unload: true, notifyDestroyed: false}); } } if(oldOBF && typeof oldOBF == "function") @@ -949,16 +949,12 @@ function Janus(gatewayCallbacks) { callbacks = callbacks || {}; // FIXME This method triggers a success even when we fail callbacks.success = (typeof callbacks.success == "function") ? callbacks.success : Janus.noop; - var asyncRequest = true; - if(callbacks.asyncRequest !== undefined && callbacks.asyncRequest !== null) - asyncRequest = (callbacks.asyncRequest === true); + var unload = (callbacks.unload === true); var notifyDestroyed = true; if(callbacks.notifyDestroyed !== undefined && callbacks.notifyDestroyed !== null) notifyDestroyed = (callbacks.notifyDestroyed === true); - var cleanupHandles = false; - if(callbacks.cleanupHandles !== undefined && callbacks.cleanupHandles !== null) - cleanupHandles = (callbacks.cleanupHandles === true); - Janus.log("Destroying session " + sessionId + " (async=" + asyncRequest + ")"); + var cleanupHandles = (callbacks.cleanupHandles === true); + Janus.log("Destroying session " + sessionId + " (unload=" + unload + ")"); if(!sessionId) { Janus.warn("No session to destroy"); callbacks.success(); @@ -972,6 +968,7 @@ function Janus(gatewayCallbacks) { } if(!connected) { Janus.warn("Is the server down? (connected=false)"); + sessionId = null; callbacks.success(); return; } @@ -981,6 +978,24 @@ function Janus(gatewayCallbacks) { request["token"] = token; if(apisecret) request["apisecret"] = apisecret; + if(unload) { + // We're unloading the page: use sendBeacon for HTTP instead, + // or just close the WebSocket connection if we're using that + if(websockets) { + ws.onclose = null; + ws.close(); + ws = null; + } else { + navigator.sendBeacon(server + "/" + sessionId, JSON.stringify(request)); + } + Janus.log("Destroyed session:"); + sessionId = null; + connected = false; + callbacks.success(); + if(notifyDestroyed) + gatewayCallbacks.destroyed(); + return; + } if(websockets) { request["session_id"] = sessionId; @@ -1020,7 +1035,6 @@ function Janus(gatewayCallbacks) { } Janus.httpAPICall(server + "/" + sessionId, { verb: 'POST', - async: asyncRequest, // Sometimes we need false here, or destroying in onbeforeunload won't work withCredentials: withCredentials, body: request, success: function(json) { @@ -1548,13 +1562,8 @@ function Janus(gatewayCallbacks) { callbacks = callbacks || {}; callbacks.success = (typeof callbacks.success == "function") ? callbacks.success : Janus.noop; callbacks.error = (typeof callbacks.error == "function") ? callbacks.error : Janus.noop; - var asyncRequest = true; - if(callbacks.asyncRequest !== undefined && callbacks.asyncRequest !== null) - asyncRequest = (callbacks.asyncRequest === true); - var noRequest = true; - if(callbacks.noRequest !== undefined && callbacks.noRequest !== null) - noRequest = (callbacks.noRequest === true); - Janus.log("Destroying handle " + handleId + " (async=" + asyncRequest + ")"); + var noRequest = (callbacks.noRequest === true); + Janus.log("Destroying handle " + handleId + " (only-locally=" + noRequest + ")"); cleanupWebrtc(handleId); var pluginHandle = pluginHandles[handleId]; if(!pluginHandle || pluginHandle.detached) { @@ -1589,7 +1598,6 @@ function Janus(gatewayCallbacks) { } Janus.httpAPICall(server + "/" + sessionId + "/" + handleId, { verb: 'POST', - async: asyncRequest, // Sometimes we need false here, or destroying in onbeforeunload won't work withCredentials: withCredentials, body: request, success: function(json) {