Commit caad85f4 authored by JC Brand's avatar JC Brand

Manually remove BOSH session tokens from cache

even if the `_converse.bosh_session` instance does not exist.

We do this to avoid trying to reconnect with stale tokens upon
reconnection. Especially relevant for anonymous connections, but could
also be applicable when switching between websocket and BOSH connections
when reconnecting.

Also renamed `startNewBOSHSession` to `startNewPreboundBOSHSession`
parent f9cc51b2
...@@ -13,6 +13,8 @@ import converse from "./converse-core"; ...@@ -13,6 +13,8 @@ import converse from "./converse-core";
const { Backbone, Strophe, _ } = converse.env; const { Backbone, Strophe, _ } = converse.env;
const BOSH_SESSION_ID = 'converse.bosh-session';
converse.plugins.add('converse-bosh', { converse.plugins.add('converse-bosh', {
...@@ -26,7 +28,7 @@ converse.plugins.add('converse-bosh', { ...@@ -26,7 +28,7 @@ converse.plugins.add('converse-bosh', {
async function initBOSHSession () { async function initBOSHSession () {
const id = 'converse.bosh-session'; const id = BOSH_SESSION_ID;
if (!_converse.bosh_session) { if (!_converse.bosh_session) {
_converse.bosh_session = new Backbone.Model({id}); _converse.bosh_session = new Backbone.Model({id});
_converse.bosh_session.browserStorage = new BrowserStorage.session(id); _converse.bosh_session.browserStorage = new BrowserStorage.session(id);
...@@ -40,7 +42,7 @@ converse.plugins.add('converse-bosh', { ...@@ -40,7 +42,7 @@ converse.plugins.add('converse-bosh', {
} }
_converse.startNewBOSHSession = function () { _converse.startNewPreboundBOSHSession = function () {
if (!_converse.prebind_url) { if (!_converse.prebind_url) {
throw new Error( throw new Error(
"attemptPreboundSession: If you use prebind then you MUST supply a prebind_url"); "attemptPreboundSession: If you use prebind then you MUST supply a prebind_url");
...@@ -97,14 +99,20 @@ converse.plugins.add('converse-bosh', { ...@@ -97,14 +99,20 @@ converse.plugins.add('converse-bosh', {
/************************ BEGIN Event Handlers ************************/ /************************ BEGIN Event Handlers ************************/
_converse.api.listen.on('clearSession', () => { _converse.api.listen.on('clearSession', () => {
if (!_.isUndefined(_converse.bosh_session)) { if (_converse.bosh_session === undefined) {
// Remove manually, even if we don't have the corresponding
// model, to avoid trying to reconnect to a stale BOSH session
const id = BOSH_SESSION_ID;
sessionStorage.removeItem(id);
sessionStorage.removeItem(`${id}-${id}`);
} else {
_converse.bosh_session.destroy(); _converse.bosh_session.destroy();
delete _converse.bosh_session; delete _converse.bosh_session;
} }
}); });
_converse.api.listen.on('setUserJID', () => { _converse.api.listen.on('setUserJID', () => {
if (!_.isUndefined(_converse.bosh_session)) { if (_converse.bosh_session !== undefined) {
_converse.bosh_session.save({'jid': _converse.jid}); _converse.bosh_session.save({'jid': _converse.jid});
} }
}); });
...@@ -127,7 +135,7 @@ converse.plugins.add('converse-bosh', { ...@@ -127,7 +135,7 @@ converse.plugins.add('converse-bosh', {
* @example _converse.api.tokens.get('rid'); * @example _converse.api.tokens.get('rid');
*/ */
get (id) { get (id) {
if (_.isUndefined(_converse.connection)) { if (_converse.connection === undefined) {
return null; return null;
} }
if (id.toLowerCase() === 'rid') { if (id.toLowerCase() === 'rid') {
......
...@@ -1494,7 +1494,7 @@ _converse.api = { ...@@ -1494,7 +1494,7 @@ _converse.api = {
if (await _converse.restoreBOSHSession()) { if (await _converse.restoreBOSHSession()) {
return; return;
} else if (_converse.authentication === _converse.PREBIND) { } else if (_converse.authentication === _converse.PREBIND) {
return _converse.startNewBOSHSession(); return _converse.startNewPreboundBOSHSession();
} }
} else if (_converse.authentication === _converse.PREBIND) { } else if (_converse.authentication === _converse.PREBIND) {
throw new Error("authentication is set to 'prebind' but we don't have a BOSH connection"); throw new Error("authentication is set to 'prebind' but we don't have a BOSH connection");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment