Commit aee23242 authored by JC Brand's avatar JC Brand

Properly tear down when logging out or disconnecting.

Remove interval handler and the user activity handler.
Make sure the connection is reset when logging out.
Also removed the reconnectTimeout, instead debounce the reconnect method by 1 second.
parent 35f222d3
......@@ -22,11 +22,11 @@ module.exports = function(grunt) {
cssmin: {
options: {
banner: "/*"+
"* Converse.js (Web-based XMPP instant messaging client) \n"+
"* http://conversejs.org \n"+
"* Copyright (c) 2012, Jan-Carel Brand <jc@opkode.com> \n"+
"* Dual licensed under the MIT and GPL Licenses \n"+
"*/"
" * Converse.js (Web-based XMPP instant messaging client) \n"+
" * http://conversejs.org \n"+
" * Copyright (c) 2012, Jan-Carel Brand <jc@opkode.com> \n"+
" * Dual licensed under the MIT and GPL Licenses \n"+
" */"
},
minify: {
dest: 'css/converse.min.css',
......
......@@ -53,6 +53,11 @@
<script>
require(['converse'], function (converse) {
converse.initialize({
auto_away: 300,
i18n: locales['af'],
auto_join_rooms: [
'discuss@conference.conversejs.org',
],
auto_reconnect: true,
bosh_service_url: 'https://conversejs.org/http-bind/', // Please use this connection manager only for testing purposes
keepalive: true,
......@@ -60,6 +65,7 @@
play_sounds: true,
roster_groups: true,
show_controlbox_by_default: true,
chatstate_notification_blacklist: ['mulles@movim.eu'],
xhr_user_search: false,
debug: true
});
......
......@@ -51,7 +51,11 @@
onDisconnected: function () {
var result = this._super.onDisconnected.apply(this, arguments);
if (result === 'disconnected') {
converse.renderLoginPanel();
converse._tearDown();
var view = converse.chatboxviews.get('controlbox');
view.model.set({connected:false});
view.$('#controlbox-tabs').empty();
view.renderLoginPanel();
}
},
......@@ -180,14 +184,6 @@
});
};
converse.renderLoginPanel = function () {
converse._tearDown();
var view = converse.chatboxviews.get('controlbox');
view.model.set({connected:false});
view.renderLoginPanel();
};
converse.ControlBoxView = converse.ChatBoxView.extend({
tagName: 'div',
className: 'chatbox',
......@@ -225,7 +221,6 @@
}))
);
if (!converse.connection.connected || !converse.connection.authenticated || converse.connection.disconnecting) {
// TODO: we might need to take prebinding into consideration here.
this.renderLoginPanel();
} else if (!this.contactspanel || !this.contactspanel.$el.is(':visible')) {
this.renderContactsPanel();
......@@ -262,15 +257,10 @@
renderLoginPanel: function () {
var $feedback = this.$('.conn-feedback'); // we want to still show any existing feedback.
var cfg = {
this.loginpanel = new converse.LoginPanel({
'$parent': this.$el.find('.controlbox-panes'),
'model': this
};
if (!this.loginpanel) {
this.loginpanel = new converse.LoginPanel(cfg);
} else {
this.loginpanel.delegateEvents().initialize(cfg);
}
});
this.loginpanel.render();
if ($feedback.length && $feedback.text() !== __('Connecting')) {
this.$('.conn-feedback').replaceWith($feedback);
......@@ -701,6 +691,9 @@
},
updateOnlineCount: _.debounce(function () {
if (typeof converse.roster === 'undefined') {
return;
}
var $count = this.$('#online-count');
$count.text('('+converse.roster.getNumOnlineContacts()+')');
if (!$count.is(':visible')) {
......
......@@ -321,7 +321,6 @@
*/
this.send_initial_presence = true;
this.msg_counter = 0;
this.reconnectTimeout = undefined;
// Module-level functions
// ----------------------
......@@ -334,50 +333,54 @@
/* Send out a Chat Status Notification (XEP-0352) */
if (converse.features[Strophe.NS.CSI] || true) {
converse.connection.send($build(stat, {xmlns: Strophe.NS.CSI}));
this.inactive = (stat === converse.INACTIVE) ? true : false;
converse.inactive = (stat === converse.INACTIVE) ? true : false;
}
};
this.onUserActivity = function () {
/* Resets counters and flags relating to CSI and auto_away/auto_xa */
if (this.idle_seconds > 0) {
this.idle_seconds = 0;
if (converse.idle_seconds > 0) {
converse.idle_seconds = 0;
}
if (!converse.connection.authenticated) {
// We can't send out any stanzas when there's no authenticated connection.
// This can happen when the connection reconnects.
// converse can happen when the connection reconnects.
return;
}
if (this.inactive) {
this.sendCSI(converse.ACTIVE);
if (converse.inactive) {
converse.sendCSI(converse.ACTIVE);
}
if (this.auto_changed_status === true) {
this.auto_changed_status = false;
this.xmppstatus.setStatus('online');
if (converse.auto_changed_status === true) {
converse.auto_changed_status = false;
converse.xmppstatus.setStatus('online');
}
};
this.onEverySecond = function () {
/* An interval handler running every second.
* Used for CSI and the auto_away and auto_xa
* features.
* Used for CSI and the auto_away and auto_xa features.
*/
if (!converse.connection.authenticated) {
// We can't send out any stanzas when there's no authenticated connection.
// This can happen when the connection reconnects.
return;
}
var stat = this.xmppstatus.getStatus();
this.idle_seconds++;
if (this.csi_waiting_time > 0 && this.idle_seconds > this.csi_waiting_time && !this.inactive) {
this.sendCSI(converse.INACTIVE);
var stat = converse.xmppstatus.getStatus();
converse.idle_seconds++;
if (converse.csi_waiting_time > 0 &&
converse.idle_seconds > converse.csi_waiting_time &&
!converse.inactive) {
converse.sendCSI(converse.INACTIVE);
}
if (this.auto_away > 0 && this.idle_seconds > this.auto_away && stat !== 'away' && stat !== 'xa') {
this.auto_changed_status = true;
this.xmppstatus.setStatus('away');
} else if (this.auto_xa > 0 && this.idle_seconds > this.auto_xa && stat !== 'xa') {
this.auto_changed_status = true;
this.xmppstatus.setStatus('xa');
if (converse.auto_away > 0 &&
converse.idle_seconds > converse.auto_away &&
stat !== 'away' && stat !== 'xa') {
converse.auto_changed_status = true;
converse.xmppstatus.setStatus('away');
} else if (converse.auto_xa > 0 &&
converse.idle_seconds > converse.auto_xa && stat !== 'xa') {
converse.auto_changed_status = true;
converse.xmppstatus.setStatus('xa');
}
};
......@@ -385,14 +388,14 @@
/* Set an interval of one second and register a handler for it.
* Required for the auto_away, auto_xa and csi_waiting_time features.
*/
if (this.auto_away < 1 && this.auto_xa < 1 && this.csi_waiting_time < 1) {
if (converse.auto_away < 1 && converse.auto_xa < 1 && converse.csi_waiting_time < 1) {
// Waiting time of less then one second means features aren't used.
return;
}
this.idle_seconds = 0;
this.auto_changed_status = false; // Was the user's status changed by converse.js?
$(window).on('click mousemove keypress focus'+unloadevent , this.onUserActivity.bind(this));
window.setInterval(this.onEverySecond.bind(this), 1000);
converse.idle_seconds = 0;
converse.auto_changed_status = false; // Was the user's status changed by converse.js?
$(window).on('click mousemove keypress focus'+unloadevent, converse.onUserActivity);
converse.everySecondTrigger = window.setInterval(converse.onEverySecond, 1000);
};
this.giveFeedback = function (message, klass) {
......@@ -421,35 +424,31 @@
};
this.reconnect = function (condition) {
this.connection.disconnect('re-connecting');
this.connection.reset();
converse.log('Attempting to reconnect in 5 seconds');
converse.giveFeedback(__('Attempting to reconnect in 5 seconds'), 'error');
window.clearTimeout(converse.reconnectTimeout);
converse.reconnectTimeout = window.setTimeout(function () {
converse.clearSession();
converse._tearDown();
if (converse.authentication !== "prebind") {
converse.attemptNonPreboundSession();
} else if (converse.prebind_url) {
converse.startNewBOSHSession();
}
}, 5000);
};
this.reconnect = _.debounce(function (condition) {
converse.connection.disconnect('re-connecting');
converse.connection.reset();
converse.log('Attempting to reconnect');
converse.giveFeedback(__('Attempting to reconnect'), 'error');
converse.clearSession();
converse._tearDown();
if (converse.authentication !== "prebind") {
converse.attemptNonPreboundSession();
} else if (converse.prebind_url) {
converse.startNewBOSHSession();
}
}, 1000);
this.onDisconnected = function (condition) {
if (!converse.auto_reconnect) { return; }
if ( converse.disconnection_cause === Strophe.Status.CONNFAIL ||
( converse.disconnection_cause === Strophe.Status.AUTHFAIL &&
converse.credentials_url &&
converse.auto_login
if (converse.disconnection_cause === Strophe.Status.CONNFAIL ||
(converse.disconnection_cause === Strophe.Status.AUTHFAIL &&
converse.credentials_url &&
converse.auto_login
)
) {
converse.reconnect(condition);
return 'reconnecting';
} else {
converse.giveFeedback(__('Disconnected'));
return 'disconnected';
}
};
......@@ -460,10 +459,6 @@
// By default we always want to send out an initial presence stanza.
converse.send_initial_presence = true;
delete converse.disconnection_cause;
if (!!converse.reconnectTimeout) {
window.clearTimeout(converse.reconnectTimeout);
delete converse.reconnectTimeout;
}
if ((typeof reconnect !== 'undefined') && (reconnect)) {
converse.log(status === Strophe.Status.CONNECTED ? 'Reconnected' : 'Reattached');
converse.onReconnected();
......@@ -488,13 +483,14 @@
converse.connection.disconnect(__('Authentication Failed'));
converse.disconnection_cause = Strophe.Status.AUTHFAIL;
} else if (status === Strophe.Status.CONNFAIL) {
converse.disconnection_cause = Strophe.Status.CONNFAIL;
} else if (status === Strophe.Status.DISCONNECTING) {
if (!converse.connection.connected) {
// FIXME: leaky abstraction from converse-controlbox.js
// Is this needed at all?
converse.renderLoginPanel();
if (converse.connection.authenticated) {
// Only set the disconnection_cause if we're still
// authenticated. If we're not, then the user logged out,
// 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');
}
......@@ -559,6 +555,7 @@
converse.chatboxviews.closeAllChatBoxes();
converse.clearSession();
converse.connection.disconnect();
converse.connection.reset();
};
this.registerGlobalEventHandlers = function () {
......@@ -1741,6 +1738,8 @@
if (this.features) {
this.features.reset();
}
$(window).off('click mousemove keypress focus'+unloadevent, converse.onUserActivity);
window.clearInterval(converse.everySecondTrigger);
return this;
};
......
......@@ -56,18 +56,12 @@
* login panel.
*/
this._super.renderLoginPanel.apply(this, arguments);
var converse = this._super.converse,
cfg;
var converse = this._super.converse;
if (converse.allow_registration) {
cfg = {
this.registerpanel = new converse.RegisterPanel({
'$parent': this.$el.find('.controlbox-panes'),
'model': this
};
if (typeof this.registerpanel === 'undefined') {
this.registerpanel = new converse.RegisterPanel(cfg);
} else {
this.registerpanel.delegateEvents().initialize(cfg);
}
});
this.registerpanel.render().$el.hide();
}
return this;
......
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