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