瀏覽代碼

Meet pings Prosody

j8
paweldomas 9 年之前
父節點
當前提交
2d4a5412c0
共有 2 個文件被更改,包括 88 次插入0 次删除
  1. 83
    0
      modules/xmpp/strophe.ping.js
  2. 5
    0
      modules/xmpp/xmpp.js

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

@@ -0,0 +1,83 @@
1
+/* global $, $iq, Strophe */
2
+
3
+var XMPPEvents = require("../../service/xmpp/XMPPEvents");
4
+
5
+var PING_INTERVAL = 15000;
6
+
7
+var PING_TIMEOUT = 10000;
8
+
9
+/**
10
+ * XEP-0199 ping plugin.
11
+ *
12
+ * Registers "urn:xmpp:ping" namespace under Strophe.NS.PING.
13
+ */
14
+module.exports = function () {
15
+    Strophe.addConnectionPlugin('ping', {
16
+
17
+        connection: null,
18
+
19
+        /**
20
+         * Initializes the plugin. Method called by Strophe.
21
+         * @param connection Strophe connection instance.
22
+         */
23
+        init: function (connection) {
24
+            this.connection = connection;
25
+            Strophe.addNamespace('PING', "urn:xmpp:ping");
26
+        },
27
+
28
+        /**
29
+         * Sends "ping" to given <tt>jid</tt>
30
+         * @param jid the JID to which ping request will be sent.
31
+         * @param success callback called on success.
32
+         * @param error callback called on error.
33
+         * @param timeout ms how long are we going to wait for the response. On
34
+         *        timeout <tt>error<//t> callback is called with undefined error
35
+         *        argument.
36
+         */
37
+        ping: function (jid, success, error, timeout) {
38
+            var iq = $iq({type: 'get', to: jid});
39
+            iq.c('ping', {xmlns: Strophe.NS.PING});
40
+            this.connection.sendIQ(iq, success, error, timeout);
41
+        },
42
+
43
+        /**
44
+         * Starts to send ping in given interval to specified remote JID.
45
+         * This plugin supports only one such task and <tt>stopInterval</tt>
46
+         * must be called before starting a new one.
47
+         * @param remoteJid remote JID to which ping requests will be sent to.
48
+         * @param interval task interval in ms.
49
+         */
50
+        startInterval: function (remoteJid, interval) {
51
+            if (this.intervalId) {
52
+                console.error("Ping task scheduled already");
53
+                return;
54
+            }
55
+            if (!interval)
56
+                interval = PING_INTERVAL;
57
+            var self = this;
58
+            this.intervalId = window.setInterval(function () {
59
+                self.ping(remoteJid,
60
+                function (result) {
61
+                    // Ping OK
62
+                },
63
+                function (error) {
64
+                    console.error(
65
+                        "Ping " + (error ? "error" : "timeout"), error);
66
+                    //FIXME: react
67
+                }, PING_TIMEOUT);
68
+            }, interval);
69
+            console.info("XMPP pings will be sent every " + interval + " ms");
70
+        },
71
+
72
+        /**
73
+         * Stops current "ping"  interval task.
74
+         */
75
+        stopInterval: function () {
76
+            if (this.intervalId) {
77
+                window.clearInterval(this.intervalId);
78
+                this.intervalId = null;
79
+                console.info("Ping interval cleared");
80
+            }
81
+        }
82
+    });
83
+};

+ 5
- 0
modules/xmpp/xmpp.js 查看文件

@@ -87,6 +87,8 @@ function connect(jid, password) {
87 87
 
88 88
                 console.info("My Jabber ID: " + connection.jid);
89 89
 
90
+                connection.ping.startInterval(config.hosts.domain);
91
+
90 92
                 if (password)
91 93
                     authenticatedUser = true;
92 94
                 maybeDoJoin();
@@ -98,6 +100,8 @@ function connect(jid, password) {
98 100
                 }
99 101
                 lastErrorMsg = msg;
100 102
             } else if (status === Strophe.Status.DISCONNECTED) {
103
+                // Stop ping interval
104
+                connection.ping.stopInterval();
101 105
                 if (anonymousConnectionFailed) {
102 106
                     // prompt user for username and password
103 107
                     XMPP.promptLogin();
@@ -167,6 +171,7 @@ function initStrophePlugins()
167 171
     require("./strophe.jingle")(XMPP, eventEmitter);
168 172
     require("./strophe.moderate")(XMPP, eventEmitter);
169 173
     require("./strophe.util")();
174
+    require("./strophe.ping")();
170 175
     require("./strophe.rayo")();
171 176
     require("./strophe.logger")();
172 177
 }

Loading…
取消
儲存