瀏覽代碼

Reacts to consecutive ping failures by closing the connection.

(cherry picked from commit 9a77ddc)
dev1
paweldomas 10 年之前
父節點
當前提交
4eb521ca7b
共有 2 個檔案被更改,包括 23 行新增5 行删除
  1. 22
    4
      modules/xmpp/strophe.ping.js
  2. 1
    1
      modules/xmpp/xmpp.js

+ 22
- 4
modules/xmpp/strophe.ping.js 查看文件

@@ -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
         }

+ 1
- 1
modules/xmpp/xmpp.js 查看文件

@@ -27,7 +27,7 @@ function initStrophePlugins(XMPP)
27 27
     require("./strophe.jingle")(XMPP, XMPP.eventEmitter);
28 28
 //    require("./strophe.moderate")(XMPP, eventEmitter);
29 29
     require("./strophe.util")();
30
-    require("./strophe.ping")();
30
+    require("./strophe.ping")(XMPP, XMPP.eventEmitter);
31 31
     require("./strophe.rayo")();
32 32
     require("./strophe.logger")();
33 33
 }

Loading…
取消
儲存