Reacts to consecutive ping failures by closing the connection.

This commit is contained in:
paweldomas 2015-08-25 12:11:31 +02:00
parent 2d4a5412c0
commit 9a77ddc54c
2 changed files with 23 additions and 5 deletions

View File

@ -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");
}
}

View File

@ -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")();
}