From 9edf7f060e95c865ac0db3e0a2fa9e295fdd70b7 Mon Sep 17 00:00:00 2001 From: paweldomas Date: Tue, 25 Aug 2015 03:25:45 -0500 Subject: [PATCH] Meet pings Prosody (cherry picked from commit 2d4a541) --- 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 4bb3287c6..00ad18d0f 100644 --- a/modules/xmpp/xmpp.js +++ b/modules/xmpp/xmpp.js @@ -27,6 +27,7 @@ function initStrophePlugins(XMPP) require("./strophe.jingle")(XMPP, XMPP.eventEmitter); // require("./strophe.moderate")(XMPP, eventEmitter); require("./strophe.util")(); + require("./strophe.ping")(); require("./strophe.rayo")(); require("./strophe.logger")(); } @@ -112,6 +113,8 @@ XMPP.prototype._connect = function (jid, password) { logger.info("My Jabber ID: " + self.connection.jid); + self.connection.ping.startInterval(config.hosts.domain); + if (password) authenticatedUser = true; if (self.connection && self.connection.connected && @@ -131,6 +134,8 @@ XMPP.prototype._connect = function (jid, password) { } lastErrorMsg = msg; } else if (status === Strophe.Status.DISCONNECTED) { + // Stop ping interval + self.connection.ping.stopInterval(); self.disconnectInProgress = false; if (anonymousConnectionFailed) { // prompt user for username and password