瀏覽代碼

Will use XEP-0199 ping for keep-alive only if it is supported by the server.

master
paweldomas 10 年之前
父節點
當前提交
60d0279e08
共有 2 個文件被更改,包括 31 次插入1 次删除
  1. 20
    0
      modules/xmpp/strophe.ping.js
  2. 11
    1
      modules/xmpp/xmpp.js

+ 20
- 0
modules/xmpp/strophe.ping.js 查看文件

@@ -53,6 +53,26 @@ module.exports = function (XMPP, eventEmitter) {
53 53
             this.connection.sendIQ(iq, success, error, timeout);
54 54
         },
55 55
 
56
+        /**
57
+         * Checks if given <tt>jid</tt> has XEP-0199 ping support.
58
+         * @param jid the JID to be checked for ping support.
59
+         * @param callback function with boolean argument which will be
60
+         * <tt>true</tt> if XEP-0199 ping is supported by given <tt>jid</tt>
61
+         */
62
+        hasPingSupport: function (jid, callback) {
63
+            this.connection.disco.info(
64
+                jid, null,
65
+                function (result) {
66
+                    var ping = $(result).find('>>feature[var="urn:xmpp:ping"]');
67
+                    callback(ping.length > 0);
68
+                },
69
+                function (error) {
70
+                    console.error("Ping feature discovery error", error);
71
+                    callback(false);
72
+                }
73
+            );
74
+        },
75
+
56 76
         /**
57 77
          * Starts to send ping in given interval to specified remote JID.
58 78
          * This plugin supports only one such task and <tt>stopInterval</tt>

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

@@ -87,7 +87,17 @@ function connect(jid, password) {
87 87
 
88 88
                 console.info("My Jabber ID: " + connection.jid);
89 89
 
90
-                connection.ping.startInterval(config.hosts.domain);
90
+                // Schedule ping ?
91
+                var pingJid = config.hosts.domain;
92
+                connection.ping.hasPingSupport(
93
+                    pingJid,
94
+                    function (hasPing) {
95
+                        if (hasPing)
96
+                            connection.ping.startInterval(pingJid);
97
+                        else
98
+                            console.warn("Ping NOT supported by " + pingJid);
99
+                    }
100
+                );
91 101
 
92 102
                 if (password)
93 103
                     authenticatedUser = true;

Loading…
取消
儲存