More work on auto-reconnecting and on maintaining sessions.

- Added the ability to reconnect to more disconnection causes.
- Make sure the roster is fetched again when reconnecting.
This commit is contained in:
JC Brand 2016-08-18 19:43:18 +00:00
parent 31f0e069ab
commit 59143c1c72
3 changed files with 52 additions and 27 deletions

View File

@ -1,5 +1,8 @@
# Changelog # Changelog
## 1.0.7 (Unreleased)
- Continuously attempt to resurrect dead connections when `auto_reconnect` is `true`. [jcbrand]
## 1.0.6 (2016-08-12) ## 1.0.6 (2016-08-12)
- #632 Offline and Logout states do not properly update once users start - #632 Offline and Logout states do not properly update once users start
chatting. [chrisuehlinger, jcband] chatting. [chrisuehlinger, jcband]

View File

@ -58,6 +58,11 @@
return result; return result;
}, },
afterReconnected: function () {
this._super.afterReconnected.apply(this, arguments);
converse.chatboxviews.get("controlbox").onConnected();
},
_tearDown: function () { _tearDown: function () {
this._super._tearDown.apply(this, arguments); this._super._tearDown.apply(this, arguments);
if (this.rosterview) { if (this.rosterview) {

View File

@ -95,6 +95,7 @@
'online': 1 'online': 1
}; };
converse.LOGIN = "login"; converse.LOGIN = "login";
converse.LOGOUT = "logout";
converse.ANONYMOUS = "anonymous"; converse.ANONYMOUS = "anonymous";
converse.PREBIND = "prebind"; converse.PREBIND = "prebind";
converse.OPENED = 'opened'; converse.OPENED = 'opened';
@ -398,6 +399,7 @@
this.reconnect = _.debounce(function (condition) { this.reconnect = _.debounce(function (condition) {
converse.connection.reconnecting = true;
converse.connection.disconnect('re-connecting'); converse.connection.disconnect('re-connecting');
converse.connection.reset(); converse.connection.reset();
converse.log('Attempting to reconnect'); converse.log('Attempting to reconnect');
@ -405,41 +407,60 @@
converse.clearSession(); converse.clearSession();
converse._tearDown(); converse._tearDown();
if (converse.authentication !== "prebind") { if (converse.authentication !== "prebind") {
converse.attemptNonPreboundSession(); converse.autoLogin();
} else if (converse.prebind_url) { } else if (converse.prebind_url) {
converse.startNewBOSHSession(); converse.startNewBOSHSession();
} }
}, 1000); }, 1000);
this.onDisconnected = function (condition) { this.onDisconnected = function (condition) {
if (converse.disconnection_cause === Strophe.Status.CONNFAIL && converse.auto_reconnect) { if (converse.disconnection_cause !== converse.LOGOUT) {
converse.reconnect(condition); if (converse.disconnection_cause === Strophe.Status.CONNFAIL && converse.auto_reconnect) {
return 'reconnecting'; converse.reconnect(condition);
} else { converse.log('RECONNECTING');
converse._tearDown(); return 'reconnecting';
converse.emit('disconnected'); } else if (
return 'disconnected'; (converse.disconnection_cause === Strophe.Status.DISCONNECTING ||
converse.disconnection_cause === Strophe.Status.DISCONNECTED) &&
converse.auto_reconnect) {
window.setTimeout(_.partial(converse.reconnect, condition), 5000);
converse.log('RECONNECTING IN 5 SECONDS');
return 'reconnecting';
}
}
delete converse.connection.reconnecting;
converse._tearDown();
converse.emit('disconnected');
converse.log('DISCONNECTED');
return 'disconnected';
};
this.setDisconnectionCause = function (connection_status) {
if (typeof converse.disconnection_cause === "undefined") {
converse.disconnection_cause = connection_status;
} }
}; };
this.onConnectStatusChanged = function (status, condition, reconnect) { this.onConnectStatusChanged = function (status, condition) {
converse.log("Status changed to: "+PRETTY_CONNECTION_STATUS[status]); converse.log("Status changed to: "+PRETTY_CONNECTION_STATUS[status]);
if (status === Strophe.Status.CONNECTED || status === Strophe.Status.ATTACHED) { if (status === Strophe.Status.CONNECTED || status === Strophe.Status.ATTACHED) {
// By default we always want to send out an initial presence stanza. // By default we always want to send out an initial presence stanza.
converse.send_initial_presence = true; converse.send_initial_presence = true;
delete converse.disconnection_cause; delete converse.disconnection_cause;
if ((typeof reconnect !== 'undefined') && (reconnect)) { if (converse.connection.reconnecting) {
converse.log(status === Strophe.Status.CONNECTED ? 'Reconnected' : 'Reattached'); converse.log(status === Strophe.Status.CONNECTED ? 'Reconnected' : 'Reattached');
converse.onReconnected(); converse.onReconnected();
} else { } else {
converse.log(status === Strophe.Status.CONNECTED ? 'Connected' : 'Attached'); converse.log(status === Strophe.Status.CONNECTED ? 'Connected' : 'Attached');
if (converse.connection.restored) { if (converse.connection.restored) {
converse.send_initial_presence = false; // No need to send an initial presence stanza when // No need to send an initial presence stanza when
// we're restoring an existing session. // we're restoring an existing session.
converse.send_initial_presence = false;
} }
converse.onConnected(); converse.onConnected();
} }
} else if (status === Strophe.Status.DISCONNECTED) { } else if (status === Strophe.Status.DISCONNECTED) {
converse.setDisconnectionCause(status);
converse.onDisconnected(condition); converse.onDisconnected(condition);
} else if (status === Strophe.Status.ERROR) { } else if (status === Strophe.Status.ERROR) {
converse.giveFeedback(__('Error'), 'error'); converse.giveFeedback(__('Error'), 'error');
@ -451,16 +472,10 @@
converse.giveFeedback(__('Authentication Failed'), 'error'); converse.giveFeedback(__('Authentication Failed'), 'error');
converse.connection.disconnect(__('Authentication Failed')); converse.connection.disconnect(__('Authentication Failed'));
converse.disconnection_cause = Strophe.Status.AUTHFAIL; converse.disconnection_cause = Strophe.Status.AUTHFAIL;
} else if (status === Strophe.Status.CONNFAIL) { } else if (status === Strophe.Status.CONNFAIL ||
if (converse.connection.authenticated) { status === Strophe.Status.DISCONNECTING) {
// Only set the disconnection_cause if we're still converse.setDisconnectionCause(status);
// authenticated. If we're not, then the user logged out, if (status === Strophe.Status.DISCONNECTING && condition) {
// and it's therefore not strictly speaking a connection
// failure (so we won't automatically reconnect).
converse.disconnection_cause = Strophe.Status.CONNFAIL;
}
} else if (status === Strophe.Status.DISCONNECTING) {
if (condition) {
converse.giveFeedback(condition, 'error'); converse.giveFeedback(condition, 'error');
} }
} }
@ -518,6 +533,7 @@
}; };
this.logOut = function () { this.logOut = function () {
converse.disconnection_cause = converse.LOGOUT;
converse.chatboxviews.closeAllChatBoxes(); converse.chatboxviews.closeAllChatBoxes();
converse.clearSession(); converse.clearSession();
if (typeof converse.connection !== 'undefined') { if (typeof converse.connection !== 'undefined') {
@ -590,10 +606,10 @@
// We need to re-register all the event handlers on the newly // We need to re-register all the event handlers on the newly
// created connection. // created connection.
var deferred = new $.Deferred(); var deferred = new $.Deferred();
this.initStatus().done(function () { converse.initStatus().done(function () {
this.afterReconnected(); converse.afterReconnected();
deferred.resolve(); deferred.resolve();
}.bind(this)); });
converse.emit('reconnected'); converse.emit('reconnected');
return deferred.promise(); return deferred.promise();
}; };
@ -1732,7 +1748,8 @@
} }
this.connection.connect(this.jid.toLowerCase(), null, this.onConnectStatusChanged); this.connection.connect(this.jid.toLowerCase(), null, this.onConnectStatusChanged);
} else if (this.authentication === converse.LOGIN) { } else if (this.authentication === converse.LOGIN) {
if (!this.password) { var password = converse.connection.pass || this.password;
if (!password) {
throw new Error("initConnection: If you use auto_login and "+ throw new Error("initConnection: If you use auto_login and "+
"authentication='login' then you also need to provide a password."); "authentication='login' then you also need to provide a password.");
} }
@ -1742,7 +1759,7 @@
} else { } else {
this.jid = Strophe.getBareJidFromJid(this.jid).toLowerCase()+'/'+resource; this.jid = Strophe.getBareJidFromJid(this.jid).toLowerCase()+'/'+resource;
} }
this.connection.connect(this.jid, this.password, this.onConnectStatusChanged); this.connection.connect(this.jid, password, this.onConnectStatusChanged);
} }
}; };