Reset XMPP connection if page refreshes

I am trying to create a stanza connection with XMPP using javascript with my ejabberd server, I can establish a connection when the page loads and may disconnect when I click the disconnect button.

now the problem is that the connection is live, and if the user accidentally refreshes the page, then my javascript tries to connect again, and in response to this I get. (its only because the previous connection may still be alive)

<body xmlns='http://jabber.org/protocol/httpbind'> <failure xmlns='urn:ietf:params:xml:ns:xmpp-sasl'> <not-authorized/> </failure> </body> 

and after a while, if I refresh the page again, the connection will be successfully established, since I can reset the connection if the page is accidentally updated, so that the user gets a seamless connection.

+2
source share
2 answers

If you want the connection to refresh the page, save the jid , sid and rid variables, as well as any other state you need, in something like sessionStorage. You can then use attach() to resume the XMPP session.

+3
source

I assume that you are logging in with the same JID, which is prohibited. You need to set a random resource for your JID to avoid collisions. Therefore, instead of registering a user with JID user@domain you should use JID user@domain /some_random_resource .

In addition, you can also listen to onbeforeunload and disable it. This will not work with web browsers, but it will work with the rest.

0
source

Source: https://habr.com/ru/post/1440472/


All Articles