2019-07-11 10:48:52 +02:00
|
|
|
/**
|
2020-01-26 16:21:20 +01:00
|
|
|
* @copyright The Converse.js contributors
|
2019-09-19 16:54:55 +02:00
|
|
|
* @license Mozilla Public License (MPLv2)
|
|
|
|
* @description Converse.js plugin which add support for XEP-0206: XMPP Over BOSH
|
2019-07-11 10:48:52 +02:00
|
|
|
*/
|
2019-09-24 15:38:34 +02:00
|
|
|
import 'strophe.js/src/bosh';
|
2021-12-10 22:31:55 +01:00
|
|
|
import log from "../log.js";
|
2022-01-25 13:10:48 +01:00
|
|
|
import { BOSH_WAIT } from '@converse/headless/shared/constants.js';
|
2020-06-08 16:08:50 +02:00
|
|
|
import { Model } from '@converse/skeletor/src/model.js';
|
2020-12-03 13:40:30 +01:00
|
|
|
import { _converse, api, converse } from "../core.js";
|
2021-12-10 22:31:55 +01:00
|
|
|
import { setUserJID, } from '@converse/headless/utils/init.js';
|
2019-06-05 10:01:55 +02:00
|
|
|
|
2019-09-19 16:54:55 +02:00
|
|
|
const { Strophe } = converse.env;
|
2019-06-05 10:01:55 +02:00
|
|
|
|
2019-07-29 10:08:38 +02:00
|
|
|
const BOSH_SESSION_ID = 'converse.bosh-session';
|
|
|
|
|
2019-06-05 10:01:55 +02:00
|
|
|
|
|
|
|
converse.plugins.add('converse-bosh', {
|
|
|
|
|
2018-11-18 19:14:22 +01:00
|
|
|
enabled () {
|
2020-06-03 17:43:57 +02:00
|
|
|
return !_converse.api.settings.get("blacklisted_plugins").includes('converse-bosh');
|
2018-11-18 19:14:22 +01:00
|
|
|
},
|
|
|
|
|
2019-06-05 10:01:55 +02:00
|
|
|
initialize () {
|
2020-06-03 09:17:13 +02:00
|
|
|
api.settings.extend({
|
2019-06-05 10:01:55 +02:00
|
|
|
bosh_service_url: undefined,
|
|
|
|
prebind_url: null
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
async function initBOSHSession () {
|
2019-07-29 10:08:38 +02:00
|
|
|
const id = BOSH_SESSION_ID;
|
2019-06-05 10:01:55 +02:00
|
|
|
if (!_converse.bosh_session) {
|
2019-09-19 16:54:55 +02:00
|
|
|
_converse.bosh_session = new Model({id});
|
2019-10-11 20:29:12 +02:00
|
|
|
_converse.bosh_session.browserStorage = _converse.createStore(id, "session");
|
2019-06-05 10:01:55 +02:00
|
|
|
await new Promise(resolve => _converse.bosh_session.fetch({'success': resolve, 'error': resolve}));
|
|
|
|
}
|
2018-11-18 19:14:22 +01:00
|
|
|
if (_converse.jid) {
|
|
|
|
if (_converse.bosh_session.get('jid') !== _converse.jid) {
|
2021-12-10 22:31:55 +01:00
|
|
|
const jid = await setUserJID(_converse.jid);
|
2018-11-18 19:14:22 +01:00
|
|
|
_converse.bosh_session.clear({'silent': true });
|
|
|
|
_converse.bosh_session.save({jid});
|
|
|
|
}
|
|
|
|
} else { // Keepalive
|
|
|
|
const jid = _converse.bosh_session.get('jid');
|
2021-12-10 22:31:55 +01:00
|
|
|
jid && await setUserJID(jid);
|
2019-06-05 10:01:55 +02:00
|
|
|
}
|
|
|
|
return _converse.bosh_session;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-29 10:08:38 +02:00
|
|
|
_converse.startNewPreboundBOSHSession = function () {
|
2020-03-31 13:15:57 +02:00
|
|
|
if (!api.settings.get('prebind_url')) {
|
2018-11-18 19:14:22 +01:00
|
|
|
throw new Error("startNewPreboundBOSHSession: If you use prebind then you MUST supply a prebind_url");
|
2019-06-05 10:01:55 +02:00
|
|
|
}
|
|
|
|
const xhr = new XMLHttpRequest();
|
2020-03-31 13:15:57 +02:00
|
|
|
xhr.open('GET', api.settings.get('prebind_url'), true);
|
2019-06-05 10:01:55 +02:00
|
|
|
xhr.setRequestHeader('Accept', 'application/json, text/javascript');
|
2018-11-18 19:14:22 +01:00
|
|
|
xhr.onload = async function () {
|
2019-06-05 10:01:55 +02:00
|
|
|
if (xhr.status >= 200 && xhr.status < 400) {
|
|
|
|
const data = JSON.parse(xhr.responseText);
|
2021-12-10 22:31:55 +01:00
|
|
|
const jid = await setUserJID(data.jid);
|
2019-06-05 10:01:55 +02:00
|
|
|
_converse.connection.attach(
|
2018-11-18 19:14:22 +01:00
|
|
|
jid,
|
2019-06-05 10:01:55 +02:00
|
|
|
data.sid,
|
|
|
|
data.rid,
|
2022-01-25 13:10:48 +01:00
|
|
|
_converse.connection.onConnectStatusChanged,
|
|
|
|
BOSH_WAIT
|
2019-06-05 10:01:55 +02:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
xhr.onerror();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
xhr.onerror = function () {
|
|
|
|
delete _converse.connection;
|
|
|
|
/**
|
|
|
|
* Triggered when fetching prebind tokens failed
|
|
|
|
* @event _converse#noResumeableBOSHSession
|
|
|
|
* @type { _converse }
|
|
|
|
* @example _converse.api.listen.on('noResumeableBOSHSession', _converse => { ... });
|
|
|
|
*/
|
2020-03-31 13:15:57 +02:00
|
|
|
api.trigger('noResumeableBOSHSession', _converse);
|
2019-06-05 10:01:55 +02:00
|
|
|
};
|
|
|
|
xhr.send();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_converse.restoreBOSHSession = async function () {
|
|
|
|
const jid = (await initBOSHSession()).get('jid');
|
2019-12-03 11:31:28 +01:00
|
|
|
if (jid && (_converse.connection._proto instanceof Strophe.Bosh)) {
|
2019-06-05 10:01:55 +02:00
|
|
|
try {
|
2020-08-20 17:03:21 +02:00
|
|
|
_converse.connection.restore(jid, _converse.connection.onConnectStatusChanged);
|
2019-06-05 10:01:55 +02:00
|
|
|
return true;
|
|
|
|
} catch (e) {
|
2020-04-23 13:40:02 +02:00
|
|
|
!_converse.isTestEnv() && log.warn("Could not restore session for jid: "+jid+" Error message: "+e.message);
|
2019-06-05 10:01:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/************************ BEGIN Event Handlers ************************/
|
2020-03-31 13:15:57 +02:00
|
|
|
api.listen.on('clearSession', () => {
|
2019-07-29 10:08:38 +02:00
|
|
|
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 {
|
2019-06-05 10:01:55 +02:00
|
|
|
_converse.bosh_session.destroy();
|
|
|
|
delete _converse.bosh_session;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-03-31 13:15:57 +02:00
|
|
|
api.listen.on('setUserJID', () => {
|
2019-07-29 10:08:38 +02:00
|
|
|
if (_converse.bosh_session !== undefined) {
|
2019-06-05 10:01:55 +02:00
|
|
|
_converse.bosh_session.save({'jid': _converse.jid});
|
|
|
|
}
|
|
|
|
});
|
2019-09-24 15:38:34 +02:00
|
|
|
|
2020-03-31 13:15:57 +02:00
|
|
|
api.listen.on('addClientFeatures', () => api.disco.own.features.add(Strophe.NS.BOSH));
|
2019-09-24 15:38:34 +02:00
|
|
|
|
2019-06-05 10:01:55 +02:00
|
|
|
/************************ END Event Handlers ************************/
|
|
|
|
|
|
|
|
|
|
|
|
/************************ BEGIN API ************************/
|
2020-03-31 13:15:57 +02:00
|
|
|
Object.assign(api, {
|
2019-06-05 10:01:55 +02:00
|
|
|
/**
|
|
|
|
* This namespace lets you access the BOSH tokens
|
|
|
|
*
|
2020-03-31 13:15:57 +02:00
|
|
|
* @namespace api.tokens
|
|
|
|
* @memberOf api
|
2019-06-05 10:01:55 +02:00
|
|
|
*/
|
|
|
|
tokens: {
|
|
|
|
/**
|
2020-03-31 13:15:57 +02:00
|
|
|
* @method api.tokens.get
|
2019-06-05 10:01:55 +02:00
|
|
|
* @param {string} [id] The type of token to return ('rid' or 'sid').
|
|
|
|
* @returns 'string' A token, either the RID or SID token depending on what's asked for.
|
|
|
|
* @example _converse.api.tokens.get('rid');
|
|
|
|
*/
|
|
|
|
get (id) {
|
2019-07-29 10:08:38 +02:00
|
|
|
if (_converse.connection === undefined) {
|
2019-06-05 10:01:55 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (id.toLowerCase() === 'rid') {
|
|
|
|
return _converse.connection.rid || _converse.connection._proto.rid;
|
|
|
|
} else if (id.toLowerCase() === 'sid') {
|
|
|
|
return _converse.connection.sid || _converse.connection._proto.sid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
/************************ end api ************************/
|
|
|
|
}
|
|
|
|
});
|