xmpp.chapril.org-conversejs/src/converse-ping.js

96 lines
3.7 KiB
JavaScript
Raw Normal View History

2016-02-19 12:38:29 +01:00
// Converse.js (A browser based XMPP chat client)
// http://conversejs.org
//
// Copyright (c) 2012-2017, Jan-Carel Brand <jc@opkode.com>
2016-02-19 12:38:29 +01:00
// Licensed under the Mozilla Public License (MPLv2)
//
/*global define */
2016-02-19 12:38:29 +01:00
/* This is a Converse.js plugin which add support for application-level pings
* as specified in XEP-0199 XMPP Ping.
*/
(function (root, factory) {
define(["converse-core", "strophe.ping"], factory);
}(this, function (converse) {
2016-02-19 12:38:29 +01:00
"use strict";
// Strophe methods for building stanzas
var Strophe = converse.env.Strophe,
_ = converse.env._;
converse.plugins.add('converse-ping', {
2016-02-19 12:38:29 +01:00
initialize: function () {
/* The initialize function gets called as soon as the plugin is
* loaded by converse.js's plugin machinery.
*/
var _converse = this._converse;
this.updateSettings({
2016-02-19 12:38:29 +01:00
ping_interval: 180 //in seconds
});
2016-02-19 12:38:29 +01:00
_converse.ping = function (jid, success, error, timeout) {
2016-02-19 12:38:29 +01:00
// XXX: We could first check here if the server advertised that
// it supports PING.
// However, some servers don't advertise while still keeping the
// connection option due to pings.
//
// var feature = _converse.features.findWhere({'var': Strophe.NS.PING});
_converse.lastStanzaDate = new Date();
if (_.isNil(jid)) {
jid = Strophe.getDomainFromJid(_converse.bare_jid);
2016-02-19 12:38:29 +01:00
}
if (_.isUndefined(timeout) ) { timeout = null; }
if (_.isUndefined(success) ) { success = null; }
if (_.isUndefined(error) ) { error = null; }
if (_converse.connection) {
_converse.connection.ping.ping(jid, success, error, timeout);
2016-02-19 12:38:29 +01:00
return true;
}
return false;
};
_converse.pong = function (ping) {
_converse.lastStanzaDate = new Date();
_converse.connection.ping.pong(ping);
2016-02-19 12:38:29 +01:00
return true;
};
_converse.registerPongHandler = function () {
_converse.connection.disco.addFeature(Strophe.NS.PING);
_converse.connection.ping.addPingHandler(_converse.pong);
2016-02-19 12:38:29 +01:00
};
_converse.registerPingHandler = function () {
_converse.registerPongHandler();
if (_converse.ping_interval > 0) {
_converse.connection.addHandler(function () {
2016-02-19 12:38:29 +01:00
/* Handler on each stanza, saves the received date
* in order to ping only when needed.
*/
_converse.lastStanzaDate = new Date();
2016-02-19 12:38:29 +01:00
return true;
});
_converse.connection.addTimedHandler(1000, function () {
2016-02-19 12:38:29 +01:00
var now = new Date();
if (!_converse.lastStanzaDate) {
_converse.lastStanzaDate = now;
2016-02-19 12:38:29 +01:00
}
if ((now - _converse.lastStanzaDate)/1000 > _converse.ping_interval) {
return _converse.ping();
2016-02-19 12:38:29 +01:00
}
return true;
});
}
};
var onConnected = function () {
// Wrapper so that we can spy on registerPingHandler in tests
_converse.registerPingHandler();
};
_converse.on('connected', onConnected);
_converse.on('reconnected', onConnected);
2016-02-19 12:38:29 +01:00
}
});
}));