Change ping plugin to not have to use overrides.

Had to add a `reconnect` event to core.
This commit is contained in:
JC Brand 2016-03-21 09:53:09 +00:00
parent 82aa9fec2c
commit 3aefba3200
4 changed files with 34 additions and 39 deletions

View File

@ -862,12 +862,14 @@ When keepalive=true but there aren't any stored prebind tokens.
``converse.listen.on('noResumeableSession', function (event) { ... });`` ``converse.listen.on('noResumeableSession', function (event) { ... });``
reconnect reconnected
~~~~~~~~~ ~~~~~~~~~~~
After the connection has dropped. Converse.js will attempt to reconnect when not in prebind mode. After the connection has dropped and converse.js has reconnected.
Any Strophe stanza handlers (as registered via `converse.listen.stanza`) will
have to be registered anew.
``converse.listen.on('reconnect', function (event) { ... });`` ``converse.listen.on('reconnected', function (event) { ... });``
roomInviteSent roomInviteSent
~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~

View File

@ -2,28 +2,34 @@
(function (root, factory) { (function (root, factory) {
define([ define([
"jquery", "jquery",
"mock",
"test_utils",
"converse-ping" "converse-ping"
], function ($, mock, test_utils) { ], function ($) {
return factory($, mock, test_utils); return factory($);
} }
); );
} (this, function ($, mock, test_utils) { } (this, function ($) {
"use strict"; "use strict";
describe("XMPP Ping", $.proxy(function (mock, test_utils) { describe("XMPP Ping", function () {
describe("Ping and pong handlers", $.proxy(function (mock, test_utils) { describe("Ping and pong handlers", function () {
it("are registered when converse.js is initialized", $.proxy(function () { it("are registered when converse.js is connected", function () {
spyOn(converse, 'registerPingHandler').andCallThrough(); spyOn(converse, 'registerPingHandler').andCallThrough();
spyOn(converse, 'registerPongHandler').andCallThrough(); spyOn(converse, 'registerPongHandler').andCallThrough();
converse._initialize(); converse.emit('connected');
expect(converse.registerPingHandler).toHaveBeenCalled(); expect(converse.registerPingHandler).toHaveBeenCalled();
expect(converse.registerPongHandler).toHaveBeenCalled(); expect(converse.registerPongHandler).toHaveBeenCalled();
}, converse, mock, test_utils)); });
}));
describe("An IQ stanza", $.proxy(function (mock, test_utils) { it("are registered when converse.js reconnected", function () {
spyOn(converse, 'registerPingHandler').andCallThrough();
spyOn(converse, 'registerPongHandler').andCallThrough();
converse.emit('reconnected');
expect(converse.registerPingHandler).toHaveBeenCalled();
expect(converse.registerPongHandler).toHaveBeenCalled();
});
});
describe("An IQ stanza", function () {
it("is sent out when converse.js pings a server", function () { it("is sent out when converse.js pings a server", function () {
var sent_stanza, IQ_id; var sent_stanza, IQ_id;
var sendIQ = converse.connection.sendIQ; var sendIQ = converse.connection.sendIQ;
@ -37,6 +43,6 @@
"<ping xmlns='urn:xmpp:ping'/>"+ "<ping xmlns='urn:xmpp:ping'/>"+
"</iq>"); "</iq>");
}); });
})); });
}, converse, mock, test_utils)); });
})); }));

View File

@ -626,6 +626,7 @@
this.afterReconnected(); this.afterReconnected();
deferred.resolve(); deferred.resolve();
}.bind(this)); }.bind(this));
converse.emit('reconnected');
return deferred.promise(); return deferred.promise();
}; };

View File

@ -24,27 +24,6 @@
converse_api.plugins.add('ping', { converse_api.plugins.add('ping', {
overrides: {
// Overrides mentioned here will be picked up by converse.js's
// plugin architecture they will replace existing methods on the
// relevant objects or classes.
//
// New functions which don't exist yet can also be added.
onConnected: function () {
var promise = this._super.onConnected();
promise.done(converse.registerPingHandler);
return promise;
},
onReconnected: function () {
// We need to re-register the ping event handler on the newly
// created connection.
var promise = this._super.onReconnected();
promise.done(converse.registerPingHandler);
return promise;
}
},
initialize: function () { initialize: function () {
/* The initialize function gets called as soon as the plugin is /* The initialize function gets called as soon as the plugin is
* loaded by converse.js's plugin machinery. * loaded by converse.js's plugin machinery.
@ -118,6 +97,13 @@
converse.ping(jid); converse.ping(jid);
} }
}); });
var onConnected = function () {
// Wrapper so that we can spy on registerPingHandler in tests
converse.registerPingHandler();
};
converse.on('connected', onConnected);
converse.on('reconnected', onConnected);
} }
}); });
})); }));