问题
I am trying to create a strophe connection with XMPP using javascript with my ejabberd server , i am able to make a connection when the page loads and able to disconnect when the disconnect button is clicked.
now the problem is , the connection is alive and if a user accidently refresh the page , then my javascript is trying to make the connect again , and in response to that i am getting. (its just because the previous connection might be still 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 then connection will get established successfully, so how can i reset the connection in case if the page is accidently refreshed so that a user get a seamless connection.
回答1:
If you want to have the connection survive across a page refresh, keep the jid
, sid
and rid
variables, along with any other state you need, in something like sessionStorage. You can then use attach() to resume the XMPP session.
回答2:
I guess you login with the same JID, which is not allowed. You need to set a random resource to your JID to ensure no collisions. So instead of logging the user with JID user@domain
you should use the JID user@domain/some_random_resource
.
Also as a matter of principle you can also listen to onbeforeunload
and disconnect there. This will not work with webkit-based browsers but will work with the rest.
来源:https://stackoverflow.com/questions/9632865/reset-the-xmpp-connection-if-the-page-gets-refreshed