Commit c3b98fd2 authored by JC Brand's avatar JC Brand

Refactor and improve the reconnecting code and make it configurable

There is still the apparent problem that after reconnecting, event listeners
hvae to bre reregistered (which doesn't happen currently).

So messages can be sent but not received...
parent d0f023db
...@@ -133,6 +133,7 @@ ...@@ -133,6 +133,7 @@
this.allow_otr = true; this.allow_otr = true;
this.animate = true; this.animate = true;
this.auto_list_rooms = false; this.auto_list_rooms = false;
this.auto_reconnect = true;
this.auto_subscribe = false; this.auto_subscribe = false;
this.bosh_service_url = undefined; // The BOSH connection manager URL. this.bosh_service_url = undefined; // The BOSH connection manager URL.
this.cache_otr_key = false; this.cache_otr_key = false;
...@@ -158,6 +159,7 @@ ...@@ -158,6 +159,7 @@
'allow_otr', 'allow_otr',
'animate', 'animate',
'auto_list_rooms', 'auto_list_rooms',
'auto_reconnect',
'auto_subscribe', 'auto_subscribe',
'bosh_service_url', 'bosh_service_url',
'cache_otr_key', 'cache_otr_key',
...@@ -315,41 +317,63 @@ ...@@ -315,41 +317,63 @@
); );
}; };
this.onConnect = function (status) { this.reconnect = function () {
converse.giveFeedback(__('Reconnecting'), 'error');
if (converse.prebind) {
this.connection.attach(
this.jid,
this.sid,
this.rid,
this.onConnect
);
} else {
this.connection.connect(
this.connection.jid,
this.connection.pass,
converse.onConnect,
this.connection.wait,
this.connection.hold,
this.connection.route
);
}
};
this.showLoginButton = function () {
var view = converse.chatboxesview.views.controlbox;
if (typeof view.loginpanel !== 'undefined') {
view.loginpanel.showLoginButton();
}
};
this.onConnect = function (status, condition) {
var $button, $form; var $button, $form;
if (status === Strophe.Status.CONNECTED) { if ((status === Strophe.Status.CONNECTED) ||
converse.log('Connected'); (status === Strophe.Status.ATTACHED)) {
converse.log(status === Strophe.Status.CONNECTED ? 'Connected' : 'Attached');
converse.onConnected(); converse.onConnected();
} else if (status === Strophe.Status.DISCONNECTED) { } else if (status === Strophe.Status.DISCONNECTED) {
$form = $('#converse-login'); // TODO: Handle case where user manually logs out...
$button = $form.find('input[type=submit]');
if ($button) { $button.show().siblings('span').remove(); }
converse.giveFeedback(__('Disconnected'), 'error'); converse.giveFeedback(__('Disconnected'), 'error');
converse.connection.connect( if (converse.auto_reconnect) {
converse.connection.jid, converse.reconnect();
converse.connection.pass, } else {
converse.onConnect converse.showLoginButton();
); }
} else if (status === Strophe.Status.Error) { } else if (status === Strophe.Status.Error) {
$form = $('#converse-login'); converse.showLoginButton();
$button = $form.find('input[type=submit]');
if ($button) { $button.show().siblings('span').remove(); }
converse.giveFeedback(__('Error'), 'error'); converse.giveFeedback(__('Error'), 'error');
} else if (status === Strophe.Status.CONNECTING) { } else if (status === Strophe.Status.CONNECTING) {
converse.giveFeedback(__('Connecting')); converse.giveFeedback(__('Connecting'));
} else if (status === Strophe.Status.CONNFAIL) { } else if (status === Strophe.Status.CONNFAIL) {
converse.chatboxesview.views.controlbox.trigger('connection-fail'); converse.showLoginButton();
converse.giveFeedback(__('Connection Failed'), 'error'); converse.giveFeedback(__('Connection Failed'), 'error');
} else if (status === Strophe.Status.AUTHENTICATING) { } else if (status === Strophe.Status.AUTHENTICATING) {
converse.giveFeedback(__('Authenticating')); converse.giveFeedback(__('Authenticating'));
} else if (status === Strophe.Status.AUTHFAIL) { } else if (status === Strophe.Status.AUTHFAIL) {
converse.chatboxesview.views.controlbox.trigger('auth-fail'); converse.showLoginButton();
converse.giveFeedback(__('Authentication Failed'), 'error'); converse.giveFeedback(__('Authentication Failed'), 'error');
} else if (status === Strophe.Status.DISCONNECTING) { } else if (status === Strophe.Status.DISCONNECTING) {
converse.giveFeedback(__('Disconnecting'), 'error'); converse.giveFeedback(__('Disconnecting'), 'error');
} else if (status === Strophe.Status.ATTACHED) {
converse.log('Attached');
converse.onConnected();
} }
}; };
...@@ -374,7 +398,7 @@ ...@@ -374,7 +398,7 @@
this.parseISO8601 = function (datestr) { this.parseISO8601 = function (datestr) {
/* Parses string formatted as 2013-02-14T11:27:08.268Z to a Date obj. /* Parses string formatted as 2013-02-14T11:27:08.268Z to a Date obj.
*/ */
    var numericKeys = [1, 4, 5, 6, 7, 10, 11], var numericKeys = [1, 4, 5, 6, 7, 10, 11],
struct = /^\s*(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}\.?\d*)Z\s*$/.exec(datestr), struct = /^\s*(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}\.?\d*)Z\s*$/.exec(datestr),
minutesOffset = 0, minutesOffset = 0,
i, k; i, k;
...@@ -3398,7 +3422,7 @@ ...@@ -3398,7 +3422,7 @@
converse.connection.connect(jid, password, converse.onConnect); converse.connection.connect(jid, password, converse.onConnect);
}, },
showConnectButton: function () { showLoginButton: function () {
var $form = this.$el.find('#converse-login'); var $form = this.$el.find('#converse-login');
var $button = $form.find('input[type=submit]'); var $button = $form.find('input[type=submit]');
if ($button.length) { if ($button.length) {
...@@ -3409,8 +3433,6 @@ ...@@ -3409,8 +3433,6 @@
initialize: function (cfg) { initialize: function (cfg) {
cfg.$parent.html(this.$el.html(this.template())); cfg.$parent.html(this.$el.html(this.template()));
this.$tabs = cfg.$parent.parent().find('#controlbox-tabs'); this.$tabs = cfg.$parent.parent().find('#controlbox-tabs');
this.model.on('connection-fail', function () { this.showConnectButton(); }, this);
this.model.on('auth-fail', function () { this.showConnectButton(); }, this);
}, },
render: function () { render: function () {
......
...@@ -747,6 +747,14 @@ For each room on the server a query is made to fetch further details (e.g. ...@@ -747,6 +747,14 @@ For each room on the server a query is made to fetch further details (e.g.
features, number of occupants etc.), so on servers with many rooms this features, number of occupants etc.), so on servers with many rooms this
option will create lots of extra connection traffic. option will create lots of extra connection traffic.
auto_reconnect
--------------
Default = ``true``
Automatically reconnect to the XMPP server if the connection drops
unexpectedly.
auto_subscribe auto_subscribe
-------------- --------------
......
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