Commit dc858cc3 authored by JC Brand's avatar JC Brand

Register connection addHandlers when reconnecting

parent c3b98fd2
...@@ -324,13 +324,17 @@ ...@@ -324,13 +324,17 @@
this.jid, this.jid,
this.sid, this.sid,
this.rid, this.rid,
this.onConnect function (status, condition) {
converse.onConnect(status, condition, true);
}
); );
} else { } else {
this.connection.connect( this.connection.connect(
this.connection.jid, this.connection.jid,
this.connection.pass, this.connection.pass,
converse.onConnect, function (status, condition) {
converse.onConnect(status, condition, true);
},
this.connection.wait, this.connection.wait,
this.connection.hold, this.connection.hold,
this.connection.route this.connection.route
...@@ -345,12 +349,17 @@ ...@@ -345,12 +349,17 @@
} }
}; };
this.onConnect = function (status, condition) { this.onConnect = function (status, condition, reconnect) {
var $button, $form; var $button, $form;
if ((status === Strophe.Status.CONNECTED) || if ((status === Strophe.Status.CONNECTED) ||
(status === Strophe.Status.ATTACHED)) { (status === Strophe.Status.ATTACHED)) {
if ((typeof reconnect !== 'undefined') && (reconnect)) {
converse.log(status === Strophe.Status.CONNECTED ? 'Reconnected' : 'Reattached');
converse.onReconnected();
} else {
converse.log(status === Strophe.Status.CONNECTED ? 'Connected' : '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) {
// TODO: Handle case where user manually logs out... // TODO: Handle case where user manually logs out...
converse.giveFeedback(__('Disconnected'), 'error'); converse.giveFeedback(__('Disconnected'), 'error');
...@@ -451,27 +460,35 @@ ...@@ -451,27 +460,35 @@
this.xmppstatus.fetch({success: callback, error: callback}); this.xmppstatus.fetch({success: callback, error: callback});
}; };
this.initRoster = function () { this.registerRosterHandler = function () {
// Set up the roster // Register handlers that depend on the roster
this.roster = new this.RosterItems();
this.roster.localStorage = new Backbone.LocalStorage(
hex_sha1('converse.rosteritems-'+converse.bare_jid));
// Register callbacks that depend on the roster
this.connection.roster.registerCallback( this.connection.roster.registerCallback(
$.proxy(this.roster.rosterHandler, this.roster), $.proxy(this.roster.rosterHandler, this.roster),
null, 'presence', null); null, 'presence', null);
};
this.registerRosterXHandler = function () {
this.connection.addHandler( this.connection.addHandler(
$.proxy(this.roster.subscribeToSuggestedItems, this.roster), $.proxy(this.roster.subscribeToSuggestedItems, this.roster),
'http://jabber.org/protocol/rosterx', 'message', null); 'http://jabber.org/protocol/rosterx', 'message', null);
};
this.registerPresenceHandler = function () {
this.connection.addHandler( this.connection.addHandler(
$.proxy(function (presence) { $.proxy(function (presence) {
this.presenceHandler(presence); this.presenceHandler(presence);
return true; return true;
}, this.roster), null, 'presence', null); }, this.roster), null, 'presence', null);
};
this.initRoster = function () {
// Set up the roster
this.roster = new this.RosterItems();
this.roster.localStorage = new Backbone.LocalStorage(
hex_sha1('converse.rosteritems-'+converse.bare_jid));
this.registerRosterHandler();
this.registerRosterXHandler();
this.registerPresenceHandler();
// No create the view which will fetch roster items from // No create the view which will fetch roster items from
// localStorage // localStorage
this.rosterview = new this.RosterView({'model':this.roster}); this.rosterview = new this.RosterView({'model':this.roster});
...@@ -495,6 +512,18 @@ ...@@ -495,6 +512,18 @@
},this)); },this));
}; };
this.onReconnected = function () {
// We need to re-register all the event handlers on the newly
// created connection.
this.initStatus($.proxy(function () {
this.registerRosterXHandler();
this.registerPresenceHandler();
this.chatboxes.registerMessageHandler();
converse.xmppstatus.sendPresence();
this.giveFeedback(__('Online Contacts'));
}, this));
};
this.onConnected = function () { this.onConnected = function () {
if (this.debug) { if (this.debug) {
this.connection.xmlInput = function (body) { console.log(body); }; this.connection.xmlInput = function (body) { console.log(body); };
...@@ -2427,6 +2456,17 @@ ...@@ -2427,6 +2456,17 @@
this.ChatBoxes = Backbone.Collection.extend({ this.ChatBoxes = Backbone.Collection.extend({
model: converse.ChatBox, model: converse.ChatBox,
registerMessageHandler: function () {
// TODO: Make this method global to converse, trigger an event
// and let messageReceived be called via a handler for that
// event.
converse.connection.addHandler(
$.proxy(function (message) {
this.messageReceived(message);
return true;
}, this), null, 'message', 'chat');
},
onConnected: function () { onConnected: function () {
this.localStorage = new Backbone.LocalStorage( this.localStorage = new Backbone.LocalStorage(
hex_sha1('converse.chatboxes-'+converse.bare_jid)); hex_sha1('converse.chatboxes-'+converse.bare_jid));
...@@ -2438,16 +2478,9 @@ ...@@ -2438,16 +2478,9 @@
} else { } else {
this.get('controlbox').save(); this.get('controlbox').save();
} }
// This will make sure the Roster is set up // This line below will make sure the Roster is set up
this.get('controlbox').set({connected:true}); this.get('controlbox').set({connected:true});
this.registerMessageHandler();
// Register message handler
converse.connection.addHandler(
$.proxy(function (message) {
this.messageReceived(message);
return true;
}, this), null, 'message', 'chat');
// Get cached chatboxes from localstorage // Get cached chatboxes from localstorage
this.fetch({ this.fetch({
add: true, add: true,
......
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