浏览代码

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

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

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

正在加载...
取消
保存