2018-06-06 11:04:23 +02:00
|
|
|
// Converse.js
|
|
|
|
// https://conversejs.org
|
2016-02-19 12:38:29 +01:00
|
|
|
//
|
2018-06-06 11:04:23 +02:00
|
|
|
// Copyright (c) 2013-2018, the Converse.js developers
|
2016-02-19 12:38:29 +01:00
|
|
|
// Licensed under the Mozilla Public License (MPLv2)
|
|
|
|
|
|
|
|
/* This is a Converse.js plugin which add support for application-level pings
|
|
|
|
* as specified in XEP-0199 XMPP Ping.
|
|
|
|
*/
|
|
|
|
(function (root, factory) {
|
2017-02-14 15:08:39 +01:00
|
|
|
define(["converse-core", "strophe.ping"], factory);
|
2016-12-20 11:42:20 +01:00
|
|
|
}(this, function (converse) {
|
2016-02-19 12:38:29 +01:00
|
|
|
"use strict";
|
|
|
|
// Strophe methods for building stanzas
|
2017-07-10 17:46:22 +02:00
|
|
|
const { Strophe, _ } = converse.env;
|
2016-02-20 16:06:12 +01:00
|
|
|
|
2016-12-20 11:42:20 +01:00
|
|
|
converse.plugins.add('converse-ping', {
|
2016-02-19 12:38:29 +01:00
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
initialize () {
|
2016-02-19 12:38:29 +01:00
|
|
|
/* The initialize function gets called as soon as the plugin is
|
|
|
|
* loaded by converse.js's plugin machinery.
|
|
|
|
*/
|
2017-07-10 17:46:22 +02:00
|
|
|
const { _converse } = this;
|
2016-03-13 17:22:42 +01:00
|
|
|
|
2017-07-05 11:03:13 +02:00
|
|
|
_converse.api.settings.update({
|
2016-02-19 12:38:29 +01:00
|
|
|
ping_interval: 180 //in seconds
|
2016-03-13 17:22:42 +01:00
|
|
|
});
|
2016-02-19 12:38:29 +01:00
|
|
|
|
2016-12-20 10:30:20 +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.
|
|
|
|
//
|
2017-07-21 12:41:16 +02:00
|
|
|
// var feature = _converse.disco_entities[_converse.domain].features.findWhere({'var': Strophe.NS.PING});
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.lastStanzaDate = new Date();
|
2017-01-26 15:49:02 +01:00
|
|
|
if (_.isNil(jid)) {
|
2016-12-20 10:30:20 +01:00
|
|
|
jid = Strophe.getDomainFromJid(_converse.bare_jid);
|
2016-02-19 12:38:29 +01:00
|
|
|
}
|
2017-01-26 15:49:02 +01:00
|
|
|
if (_.isUndefined(timeout) ) { timeout = null; }
|
|
|
|
if (_.isUndefined(success) ) { success = null; }
|
|
|
|
if (_.isUndefined(error) ) { error = null; }
|
2016-12-20 10:30:20 +01:00
|
|
|
if (_converse.connection) {
|
|
|
|
_converse.connection.ping.ping(jid, success, error, timeout);
|
2016-02-19 12:38:29 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.pong = function (ping) {
|
|
|
|
_converse.lastStanzaDate = new Date();
|
|
|
|
_converse.connection.ping.pong(ping);
|
2016-02-19 12:38:29 +01:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.registerPongHandler = function () {
|
2017-08-16 16:01:31 +02:00
|
|
|
if (!_.isUndefined(_converse.connection.disco)) {
|
2018-05-11 13:31:48 +02:00
|
|
|
_converse.api.disco.own.features.add(Strophe.NS.PING);
|
2017-08-16 16:01:31 +02:00
|
|
|
}
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.connection.ping.addPingHandler(_converse.pong);
|
2016-02-19 12:38:29 +01:00
|
|
|
};
|
|
|
|
|
2016-12-20 10:30:20 +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.
|
|
|
|
*/
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.lastStanzaDate = new Date();
|
2016-02-19 12:38:29 +01:00
|
|
|
return true;
|
|
|
|
});
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.connection.addTimedHandler(1000, function () {
|
2017-07-10 17:46:22 +02:00
|
|
|
const now = new Date();
|
2016-12-20 10:30:20 +01:00
|
|
|
if (!_converse.lastStanzaDate) {
|
|
|
|
_converse.lastStanzaDate = now;
|
2016-02-19 12:38:29 +01:00
|
|
|
}
|
2016-12-20 10:30:20 +01:00
|
|
|
if ((now - _converse.lastStanzaDate)/1000 > _converse.ping_interval) {
|
|
|
|
return _converse.ping();
|
2016-02-19 12:38:29 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
const onConnected = function () {
|
2016-03-21 10:53:09 +01:00
|
|
|
// Wrapper so that we can spy on registerPingHandler in tests
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.registerPingHandler();
|
2016-03-21 10:53:09 +01:00
|
|
|
};
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.on('connected', onConnected);
|
|
|
|
_converse.on('reconnected', onConnected);
|
2016-02-19 12:38:29 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}));
|