Meet pings Prosody

This commit is contained in:
paweldomas 2015-08-25 10:25:45 +02:00
parent a2b43843b7
commit 2d4a5412c0
2 changed files with 88 additions and 0 deletions

View File

@ -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 <tt>jid</tt>
* @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 <tt>error<//t> 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 <tt>stopInterval</tt>
* 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");
}
}
});
};

View File

@ -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")();
}