From 2d4a5412c0ec449672bb60e0e244dcb6345eb6db Mon Sep 17 00:00:00 2001 From: paweldomas Date: Tue, 25 Aug 2015 10:25:45 +0200 Subject: [PATCH] Meet pings Prosody --- modules/xmpp/strophe.ping.js | 83 ++++++++++++++++++++++++++++++++++++ modules/xmpp/xmpp.js | 5 +++ 2 files changed, 88 insertions(+) create mode 100644 modules/xmpp/strophe.ping.js diff --git a/modules/xmpp/strophe.ping.js b/modules/xmpp/strophe.ping.js new file mode 100644 index 000000000..05dd029cc --- /dev/null +++ b/modules/xmpp/strophe.ping.js @@ -0,0 +1,83 @@ +/* global $, $iq, Strophe */ + +var XMPPEvents = require("../../service/xmpp/XMPPEvents"); + +var PING_INTERVAL = 15000; + +var PING_TIMEOUT = 10000; + +/** + * XEP-0199 ping plugin. + * + * Registers "urn:xmpp:ping" namespace under Strophe.NS.PING. + */ +module.exports = function () { + Strophe.addConnectionPlugin('ping', { + + connection: null, + + /** + * Initializes the plugin. Method called by Strophe. + * @param connection Strophe connection instance. + */ + init: function (connection) { + this.connection = connection; + Strophe.addNamespace('PING', "urn:xmpp:ping"); + }, + + /** + * Sends "ping" to given jid + * @param jid the JID to which ping request will be sent. + * @param success callback called on success. + * @param error callback called on error. + * @param timeout ms how long are we going to wait for the response. On + * timeout error callback is called with undefined error + * argument. + */ + ping: function (jid, success, error, timeout) { + var iq = $iq({type: 'get', to: jid}); + iq.c('ping', {xmlns: Strophe.NS.PING}); + this.connection.sendIQ(iq, success, error, timeout); + }, + + /** + * Starts to send ping in given interval to specified remote JID. + * This plugin supports only one such task and stopInterval + * must be called before starting a new one. + * @param remoteJid remote JID to which ping requests will be sent to. + * @param interval task interval in ms. + */ + startInterval: function (remoteJid, interval) { + if (this.intervalId) { + console.error("Ping task scheduled already"); + return; + } + if (!interval) + interval = PING_INTERVAL; + var self = this; + this.intervalId = window.setInterval(function () { + self.ping(remoteJid, + function (result) { + // Ping OK + }, + function (error) { + console.error( + "Ping " + (error ? "error" : "timeout"), error); + //FIXME: react + }, PING_TIMEOUT); + }, interval); + console.info("XMPP pings will be sent every " + interval + " ms"); + }, + + /** + * Stops current "ping" interval task. + */ + stopInterval: function () { + if (this.intervalId) { + window.clearInterval(this.intervalId); + this.intervalId = null; + console.info("Ping interval cleared"); + } + } + }); +}; diff --git a/modules/xmpp/xmpp.js b/modules/xmpp/xmpp.js index 2e87ab201..a904bdf63 100644 --- a/modules/xmpp/xmpp.js +++ b/modules/xmpp/xmpp.js @@ -87,6 +87,8 @@ function connect(jid, password) { console.info("My Jabber ID: " + connection.jid); + connection.ping.startInterval(config.hosts.domain); + if (password) authenticatedUser = true; maybeDoJoin(); @@ -98,6 +100,8 @@ function connect(jid, password) { } lastErrorMsg = msg; } else if (status === Strophe.Status.DISCONNECTED) { + // Stop ping interval + connection.ping.stopInterval(); if (anonymousConnectionFailed) { // prompt user for username and password XMPP.promptLogin(); @@ -167,6 +171,7 @@ function initStrophePlugins() require("./strophe.jingle")(XMPP, eventEmitter); require("./strophe.moderate")(XMPP, eventEmitter); require("./strophe.util")(); + require("./strophe.ping")(); require("./strophe.rayo")(); require("./strophe.logger")(); }