|
@@ -2,20 +2,33 @@
|
2
|
2
|
|
3
|
3
|
var XMPPEvents = require("../../service/xmpp/XMPPEvents");
|
4
|
4
|
|
5
|
|
-var PING_INTERVAL = 15000;
|
|
5
|
+/**
|
|
6
|
+ * Ping every 20 sec
|
|
7
|
+ */
|
|
8
|
+var PING_INTERVAL = 20000;
|
|
9
|
+
|
|
10
|
+/**
|
|
11
|
+ * Ping timeout error after 15 sec of waiting.
|
|
12
|
+ */
|
|
13
|
+var PING_TIMEOUT = 15000;
|
6
|
14
|
|
7
|
|
-var PING_TIMEOUT = 10000;
|
|
15
|
+/**
|
|
16
|
+ * Will close the connection after 3 consecutive ping errors.
|
|
17
|
+ */
|
|
18
|
+var PING_THRESHOLD = 3;
|
8
|
19
|
|
9
|
20
|
/**
|
10
|
21
|
* XEP-0199 ping plugin.
|
11
|
22
|
*
|
12
|
23
|
* Registers "urn:xmpp:ping" namespace under Strophe.NS.PING.
|
13
|
24
|
*/
|
14
|
|
-module.exports = function () {
|
|
25
|
+module.exports = function (XMPP, eventEmitter) {
|
15
|
26
|
Strophe.addConnectionPlugin('ping', {
|
16
|
27
|
|
17
|
28
|
connection: null,
|
18
|
29
|
|
|
30
|
+ failedPings: 0,
|
|
31
|
+
|
19
|
32
|
/**
|
20
|
33
|
* Initializes the plugin. Method called by Strophe.
|
21
|
34
|
* @param connection Strophe connection instance.
|
|
@@ -59,11 +72,15 @@ module.exports = function () {
|
59
|
72
|
self.ping(remoteJid,
|
60
|
73
|
function (result) {
|
61
|
74
|
// Ping OK
|
|
75
|
+ self.failedPings = 0;
|
62
|
76
|
},
|
63
|
77
|
function (error) {
|
|
78
|
+ self.failedPings += 1;
|
64
|
79
|
console.error(
|
65
|
80
|
"Ping " + (error ? "error" : "timeout"), error);
|
66
|
|
- //FIXME: react
|
|
81
|
+ if (self.failedPings >= PING_THRESHOLD) {
|
|
82
|
+ self.connection.disconnect();
|
|
83
|
+ }
|
67
|
84
|
}, PING_TIMEOUT);
|
68
|
85
|
}, interval);
|
69
|
86
|
console.info("XMPP pings will be sent every " + interval + " ms");
|
|
@@ -76,6 +93,7 @@ module.exports = function () {
|
76
|
93
|
if (this.intervalId) {
|
77
|
94
|
window.clearInterval(this.intervalId);
|
78
|
95
|
this.intervalId = null;
|
|
96
|
+ this.failedPings = 0;
|
79
|
97
|
console.info("Ping interval cleared");
|
80
|
98
|
}
|
81
|
99
|
}
|