Skip to content

Commit

Permalink
Use sendBeacon instead of sync XHR in onbeforeunload (fixes #1902) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lminiero authored Jan 10, 2020
1 parent 06e76a8 commit b51d935
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions html/janus.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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();
Expand All @@ -972,6 +968,7 @@ function Janus(gatewayCallbacks) {
}
if(!connected) {
Janus.warn("Is the server down? (connected=false)");
sessionId = null;
callbacks.success();
return;
}
Expand All @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit b51d935

Please sign in to comment.