Disable Strophe Connection on Page Unloading

I wrote a MUC web client using strophe.js and jQuery, and I'm trying to send an inaccessible presence in the room and disable the user in jquery to unload the event for the window. If the user moves or closes the browser, they must be exited from the MUC.

I tested the code that I run in this case through logging out on my page, so I'm sure the stanza is correct. I think strophe is having trouble sending the stanza if the browser window closes. Is there any workaround? I also tried onbeforeunload (I know that it is not fully cross browser compatible), but this does not work either.

Any advice is greatly appreciated!

Thanks, John

+6
jquery xmpp strophe
source share
2 answers

Make sure you switch to sync mode and call flush() before sending disconnect() . 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(); } } 

You can then bind to onbeforeunload / onunload. (jQuery example)

 var client = new Client; client.connect(); $(window).unload(function() { client.disconnect(); }); 
+10
source share

There are no fixes, now there is a built-in solution:

 connection.flush(); connection._options.sync = true; connection.disconnect(); 
+2
source share

All Articles