Reacts to consecutive ping failures by closing the connection.
This commit is contained in:
parent
2d4a5412c0
commit
9a77ddc54c
|
@ -2,20 +2,33 @@
|
||||||
|
|
||||||
var XMPPEvents = require("../../service/xmpp/XMPPEvents");
|
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.
|
* XEP-0199 ping plugin.
|
||||||
*
|
*
|
||||||
* Registers "urn:xmpp:ping" namespace under Strophe.NS.PING.
|
* Registers "urn:xmpp:ping" namespace under Strophe.NS.PING.
|
||||||
*/
|
*/
|
||||||
module.exports = function () {
|
module.exports = function (XMPP, eventEmitter) {
|
||||||
Strophe.addConnectionPlugin('ping', {
|
Strophe.addConnectionPlugin('ping', {
|
||||||
|
|
||||||
connection: null,
|
connection: null,
|
||||||
|
|
||||||
|
failedPings: 0,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes the plugin. Method called by Strophe.
|
* Initializes the plugin. Method called by Strophe.
|
||||||
* @param connection Strophe connection instance.
|
* @param connection Strophe connection instance.
|
||||||
|
@ -59,11 +72,15 @@ module.exports = function () {
|
||||||
self.ping(remoteJid,
|
self.ping(remoteJid,
|
||||||
function (result) {
|
function (result) {
|
||||||
// Ping OK
|
// Ping OK
|
||||||
|
self.failedPings = 0;
|
||||||
},
|
},
|
||||||
function (error) {
|
function (error) {
|
||||||
|
self.failedPings += 1;
|
||||||
console.error(
|
console.error(
|
||||||
"Ping " + (error ? "error" : "timeout"), error);
|
"Ping " + (error ? "error" : "timeout"), error);
|
||||||
//FIXME: react
|
if (self.failedPings >= PING_THRESHOLD) {
|
||||||
|
self.connection.disconnect();
|
||||||
|
}
|
||||||
}, PING_TIMEOUT);
|
}, PING_TIMEOUT);
|
||||||
}, interval);
|
}, interval);
|
||||||
console.info("XMPP pings will be sent every " + interval + " ms");
|
console.info("XMPP pings will be sent every " + interval + " ms");
|
||||||
|
@ -76,6 +93,7 @@ module.exports = function () {
|
||||||
if (this.intervalId) {
|
if (this.intervalId) {
|
||||||
window.clearInterval(this.intervalId);
|
window.clearInterval(this.intervalId);
|
||||||
this.intervalId = null;
|
this.intervalId = null;
|
||||||
|
this.failedPings = 0;
|
||||||
console.info("Ping interval cleared");
|
console.info("Ping interval cleared");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -171,7 +171,7 @@ function initStrophePlugins()
|
||||||
require("./strophe.jingle")(XMPP, eventEmitter);
|
require("./strophe.jingle")(XMPP, eventEmitter);
|
||||||
require("./strophe.moderate")(XMPP, eventEmitter);
|
require("./strophe.moderate")(XMPP, eventEmitter);
|
||||||
require("./strophe.util")();
|
require("./strophe.util")();
|
||||||
require("./strophe.ping")();
|
require("./strophe.ping")(XMPP, eventEmitter);
|
||||||
require("./strophe.rayo")();
|
require("./strophe.rayo")();
|
||||||
require("./strophe.logger")();
|
require("./strophe.logger")();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue