I've written a web-based MUC client using strophe.js and jQuery and I'm trying to send an unavailable presence to the room and disconnect the user in the jquery unload event for the window. If a user navigates away or closes the browser tab, they should be logged out of the MUC.
I've tested the code that I'm running in this event through a logout button I have on the page, so I'm pretty sure the stanza is correct. I think strophe is having trouble sending the stanza if the browser window is closing. Is there any workaround here? I've also tried the onbeforeunload event (I know it's not entirely cross-browser compatible), but that doesn't seem to work either.
Any advice is much appreciated!
Thanks, John
Ensure you switch to sync mode, and call flush()
before sending your disconnect()
call. Here is an example:
var Client = {
connect: function(spec) {
this.connection = new Strophe.Connection(spec.url);
},
disconnect: function() {
this.connection.options.sync = true; // Switch to using synchronous requests since this is typically called onUnload.
this.connection.flush();
this.connection.disconnect();
}
}
Then you can bind to onbeforeunload/onunload. (jQuery example)
var client = new Client;
client.connect();
$(window).unload(function() {
client.disconnect();
});
No patches needed, there's now a built-in solution:
connection.flush();
connection._options.sync = true;
connection.disconnect();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With