diff --git a/modules/xmpp/strophe.ping.js b/modules/xmpp/strophe.ping.js index 05dd029cc..b11f65119 100644 --- a/modules/xmpp/strophe.ping.js +++ b/modules/xmpp/strophe.ping.js @@ -2,20 +2,33 @@ var XMPPEvents = require("../../service/xmpp/XMPPEvents"); -var PING_INTERVAL = 15000; +/** + * Ping every 20 sec + */ +var PING_INTERVAL = 20000; -var PING_TIMEOUT = 10000; +/** + * Ping timeout error after 15 sec of waiting. + */ +var PING_TIMEOUT = 15000; + +/** + * Will close the connection after 3 consecutive ping errors. + */ +var PING_THRESHOLD = 3; /** * XEP-0199 ping plugin. * * Registers "urn:xmpp:ping" namespace under Strophe.NS.PING. */ -module.exports = function () { +module.exports = function (XMPP, eventEmitter) { Strophe.addConnectionPlugin('ping', { connection: null, + failedPings: 0, + /** * Initializes the plugin. Method called by Strophe. * @param connection Strophe connection instance. @@ -59,11 +72,15 @@ module.exports = function () { self.ping(remoteJid, function (result) { // Ping OK + self.failedPings = 0; }, function (error) { + self.failedPings += 1; console.error( "Ping " + (error ? "error" : "timeout"), error); - //FIXME: react + if (self.failedPings >= PING_THRESHOLD) { + self.connection.disconnect(); + } }, PING_TIMEOUT); }, interval); console.info("XMPP pings will be sent every " + interval + " ms"); @@ -76,6 +93,7 @@ module.exports = function () { if (this.intervalId) { window.clearInterval(this.intervalId); this.intervalId = null; + this.failedPings = 0; console.info("Ping interval cleared"); } } diff --git a/modules/xmpp/xmpp.js b/modules/xmpp/xmpp.js index a904bdf63..4afd060c7 100644 --- a/modules/xmpp/xmpp.js +++ b/modules/xmpp/xmpp.js @@ -171,7 +171,7 @@ function initStrophePlugins() require("./strophe.jingle")(XMPP, eventEmitter); require("./strophe.moderate")(XMPP, eventEmitter); require("./strophe.util")(); - require("./strophe.ping")(); + require("./strophe.ping")(XMPP, eventEmitter); require("./strophe.rayo")(); require("./strophe.logger")(); }