From f30d415f76d5fdb8e53aaf32268b61abeadc5887 Mon Sep 17 00:00:00 2001 From: JC Brand Date: Sat, 1 Jun 2019 19:36:23 +0200 Subject: [PATCH] Refactor reconnection --- CHANGES.md | 1 + src/converse-chatview.js | 2 +- src/headless/converse-core.js | 77 ++++++++++++++++++++--------------- 3 files changed, 47 insertions(+), 33 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 2a31d8ffe..ebcfaf7c8 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -52,6 +52,7 @@ - `_converse.updateSettings` has been removed in favor of [\_converse.api.settings.update](https://conversejs.org/docs/html/api/-_converse.api.settings.html#.update) - `_converse.api.roster.get` now returns a promise. - New API method [\_converse.api.disco.features.get](https://conversejs.org/docs/html/api/-_converse.api.disco.features.html#.get) +- New API method [\_converse.api.connection.reconnect](https://conversejs.org/docs/html/api/-_converse.api.connection.html#.reconnect) ## 4.2.0 (2019-04-04) diff --git a/src/converse-chatview.js b/src/converse-chatview.js index 30706995c..ba66f3e50 100644 --- a/src/converse-chatview.js +++ b/src/converse-chatview.js @@ -917,7 +917,7 @@ converse.plugins.add('converse-chatview', { ['Sorry, the connection has been lost, and your message could not be sent'], 'error' ); - _converse.reconnect(); + _converse.api.connection.reconnect(); return; } let spoiler_hint, hint_el = {}; diff --git a/src/headless/converse-core.js b/src/headless/converse-core.js index 683b34deb..ac132fd6b 100644 --- a/src/headless/converse-core.js +++ b/src/headless/converse-core.js @@ -421,6 +421,28 @@ function tearDown () { } +function reconnect () { + _converse.log('RECONNECTING: the connection has dropped, attempting to reconnect.'); + _converse.setConnectionStatus( + Strophe.Status.RECONNECTING, + __('The connection has dropped, attempting to reconnect.') + ); + /** + * Triggered when the connection has dropped, but Converse will attempt + * to reconnect again. + * + * @event _converse#will-reconnect + */ + _converse.api.trigger('will-reconnect'); + + _converse.connection.reconnecting = true; + tearDown(); + _converse.api.user.login(null, null, true); +} + +const debouncedReconnect = _.debounce(reconnect, 2000); + + function clearSession () { if (!_.isUndefined(_converse.bosh_session)) { _converse.bosh_session.destroy(); @@ -834,33 +856,6 @@ _converse.initialize = async function (settings, callback) { }; - /** - * Called once the XMPP connection has dropped and we want to attempt - * reconnection. - * @method reconnect - * @private - * @memberOf _converse - */ - this.reconnect = _.debounce(() => { - _converse.log('RECONNECTING: the connection has dropped, attempting to reconnect.'); - _converse.setConnectionStatus( - Strophe.Status.RECONNECTING, - __('The connection has dropped, attempting to reconnect.') - ); - /** - * Triggered when the connection has dropped, but Converse will attempt - * to reconnect again. - * - * @event _converse#will-reconnect - */ - _converse.api.trigger('will-reconnect'); - - _converse.connection.reconnecting = true; - tearDown(); - _converse.api.user.login(null, null, true); - }, 2000); - - /** * Properly tear down the session so that it's possible to manually connect again. * @method finishDisconnection @@ -898,7 +893,7 @@ _converse.initialize = async function (settings, callback) { /* In this case, we reconnect, because we might be receiving * expirable tokens from the credentials_url. */ - return _converse.reconnect(); + return _converse.api.connection.reconnect(); } else { return _converse.finishDisconnection(); } @@ -909,7 +904,7 @@ _converse.initialize = async function (settings, callback) { !_converse.auto_reconnect) { return _converse.finishDisconnection(); } - _converse.reconnect(); + _converse.api.connection.reconnect(); }; @@ -1426,22 +1421,23 @@ _converse.api = { * @namespace _converse.api.connection * @memberOf _converse.api */ - 'connection': { + connection: { /** * @method _converse.api.connection.connected * @memberOf _converse.api.connection * @returns {boolean} Whether there is an established connection or not. */ - 'connected' () { + connected () { return _converse.connection && _converse.connection.connected || false; }, + /** * Terminates the connection. * * @method _converse.api.connection.disconnect * @memberOf _converse.api.connection */ - 'disconnect' () { + disconnect () { if (_converse.connection) { _converse.connection.disconnect(); } else { @@ -1449,6 +1445,22 @@ _converse.api = { clearSession(); } }, + + /** + * Can be called once the XMPP connection has dropped and we want + * to attempt reconnection. + * Only needs to be called once, if reconnect fails Converse will + * attempt to reconnect every two seconds. + * @method reconnect + * @memberOf _converse.api.connection + */ + reconnect () { + if (_converse.connfeedback.get('connection_status') === Strophe.Status.RECONNECTING) { + debouncedReconnect(); + } else { + reconnect(); + } + } }, /** @@ -1495,6 +1507,7 @@ _converse.api = { 'jid' () { return _converse.connection.jid; }, + /** * Logs the user in. *