Commit c4698613 authored by JC Brand's avatar JC Brand

Merge pull request #414 from thierrytiti/ping-pong

XEP-0199: XMPP Ping
parents 7c21341d fb589398
...@@ -276,6 +276,7 @@ ...@@ -276,6 +276,7 @@
keepalive: false, keepalive: false,
message_carbons: false, message_carbons: false,
no_trimming: false, // Set to true for phantomjs tests (where browser apparently has no width) no_trimming: false, // Set to true for phantomjs tests (where browser apparently has no width)
ping_interval: 180, //in seconds
play_sounds: false, play_sounds: false,
sounds_path: '/sounds/', sounds_path: '/sounds/',
password: undefined, password: undefined,
...@@ -663,10 +664,57 @@ ...@@ -663,10 +664,57 @@
},this), 200)); },this), 200));
}; };
this.ping = function (jid, success, error, timeout){
//var feature = converse.features.findWhere({'var': Strophe.NS.PING});
//if (feature) {
converse.lastMessage=new Date();
if (typeof jid === 'undefined' || jid == null) { jid = converse.bare_jid; }
if (typeof timeout === 'undefined' ) { timeout = null; }
if (typeof success === 'undefined' ) { success = null; }
if (typeof error === 'undefined' ) { error = null; }
if (converse.connection) {
converse.connection.ping.ping( jid, success, error, timeout );
return true
};
//}
return false;
};
this.pong = function (ping){
converse.lastMessage=new Date();
converse.connection.ping.pong(ping);
return true;
};
this.registerPongHandler = function (){
var feature = converse.features.findWhere({'var': Strophe.NS.PING});
if (feature) {
converse.connection.disco.addFeature(Strophe.NS.PING);
converse.connection.ping.addPingHandler( this.pong );
}
};
this.registerPingHandler = function (){
if (this.ping_interval>0){
//handler on each message : save last message date in order to ping only when needed
converse.connection.addHandler(function(){ converse.lastMessage=new Date();});
converse.connection.addTimedHandler(1000,function() {
now = new Date();
if (!converse.lastMessage) converse.lastMessage=now;
if ((now - converse.lastMessage)/1000 > converse.ping_interval){
return converse.ping();
}
return true;
});
}
};
this.onReconnected = function () { this.onReconnected = function () {
// We need to re-register all the event handlers on the newly // We need to re-register all the event handlers on the newly
// created connection. // created connection.
this.initStatus($.proxy(function () { this.initStatus($.proxy(function () {
this.registerPongHandler();
this.registerPingHandler();
this.rosterview.registerRosterXHandler(); this.rosterview.registerRosterXHandler();
this.rosterview.registerPresenceHandler(); this.rosterview.registerPresenceHandler();
this.chatboxes.registerMessageHandler(); this.chatboxes.registerMessageHandler();
...@@ -713,6 +761,8 @@ ...@@ -713,6 +761,8 @@
this.enableCarbons(); this.enableCarbons();
this.initStatus($.proxy(function () { this.initStatus($.proxy(function () {
this.registerPingHandler();
this.registerPongHandler();
this.chatboxes.onConnected(); this.chatboxes.onConnected();
this.giveFeedback(__('Contacts')); this.giveFeedback(__('Contacts'));
if (this.callback) { if (this.callback) {
...@@ -5828,6 +5878,9 @@ ...@@ -5828,6 +5878,9 @@
'send': function (stanza) { 'send': function (stanza) {
converse.connection.send(stanza); converse.connection.send(stanza);
}, },
'ping': function (jid) {
converse.ping(jid);
},
'plugins': { 'plugins': {
'add': function (name, callback) { 'add': function (name, callback) {
converse.plugins[name] = callback; converse.plugins[name] = callback;
......
...@@ -16,6 +16,7 @@ Changelog ...@@ -16,6 +16,7 @@ Changelog
* I18N: Autodetection of User Locale if no i18n setting is set. [thierrytiti] * I18N: Autodetection of User Locale if no i18n setting is set. [thierrytiti]
* CSS: Fonts Path: editabable $font-path via sass/variables.scss [thierrytiti] * CSS: Fonts Path: editabable $font-path via sass/variables.scss [thierrytiti]
* Add offline pretty status to enable translation [thierrytiti] * Add offline pretty status to enable translation [thierrytiti]
* Add Ping funcionnality and Pong Handler (Fix to issue #144) [thierrytiti]
0.9.3 (2015-05-01) 0.9.3 (2015-05-01)
------------------ ------------------
......
...@@ -368,6 +368,17 @@ Specify the locale/language. The language must be in the ``locales`` object. Ref ...@@ -368,6 +368,17 @@ Specify the locale/language. The language must be in the ``locales`` object. Ref
.. _`play-sounds`: .. _`play-sounds`:
ping_interval
-------------
* Default: ``300``
Make ping to server in order to keep connection with server killing sessions after idle timeout.
The ping are sent only if no messages are sent in the last ``ping_interval`` seconds
You need to set the value to any positive value to enable this functionality.
If you set this value to ``0`` or any negative value, il will disable this functionality.
play_sounds play_sounds
----------- -----------
......
...@@ -38,6 +38,7 @@ require.config({ ...@@ -38,6 +38,7 @@ require.config({
"strophe.disco": "components/strophejs-plugins/disco/strophe.disco", "strophe.disco": "components/strophejs-plugins/disco/strophe.disco",
"strophe.roster": "src/strophe.roster", "strophe.roster": "src/strophe.roster",
"strophe.vcard": "src/strophe.vcard", "strophe.vcard": "src/strophe.vcard",
"strophe.ping": "src/strophe.ping",
"text": 'components/requirejs-text/text', "text": 'components/requirejs-text/text',
"tpl": 'components/requirejs-tpl-jcbrand/tpl', "tpl": 'components/requirejs-tpl-jcbrand/tpl',
"typeahead": "components/typeahead.js/index", "typeahead": "components/typeahead.js/index",
...@@ -170,7 +171,8 @@ require.config({ ...@@ -170,7 +171,8 @@ require.config({
'strophe.disco': { deps: ['strophe'] }, 'strophe.disco': { deps: ['strophe'] },
'strophe.register': { deps: ['strophe'] }, 'strophe.register': { deps: ['strophe'] },
'strophe.roster': { deps: ['strophe'] }, 'strophe.roster': { deps: ['strophe'] },
'strophe.vcard': { deps: ['strophe'] } 'strophe.vcard': { deps: ['strophe'] },
'strophe.ping': { deps: ['strophe'] }
} }
}); });
......
...@@ -35,6 +35,7 @@ ...@@ -35,6 +35,7 @@
<script type="text/javascript" src="src/strophe.roster.js"></script> <script type="text/javascript" src="src/strophe.roster.js"></script>
<script type="text/javascript" src="components/strophejs-plugins/vcard/strophe.vcard.js"></script> <script type="text/javascript" src="components/strophejs-plugins/vcard/strophe.vcard.js"></script>
<script type="text/javascript" src="components/strophejs-plugins/disco/strophe.disco.js"></script> <script type="text/javascript" src="components/strophejs-plugins/disco/strophe.disco.js"></script>
<script type="text/javascript" src="src/strophe.ping.js"></script>
<script type="text/javascript" src="components/underscore/underscore.js"></script> <script type="text/javascript" src="components/underscore/underscore.js"></script>
<script type="text/javascript" src="components/backbone//backbone.js"></script> <script type="text/javascript" src="components/backbone//backbone.js"></script>
<script type="text/javascript" src="components/backbone.browserStorage/backbone.browserStorage.js"></script> <script type="text/javascript" src="components/backbone.browserStorage/backbone.browserStorage.js"></script>
......
...@@ -6,6 +6,7 @@ define("converse-dependencies", [ ...@@ -6,6 +6,7 @@ define("converse-dependencies", [
"strophe", "strophe",
"strophe.vcard", "strophe.vcard",
"strophe.disco", "strophe.disco",
"strophe.ping",
"backbone.browserStorage", "backbone.browserStorage",
"backbone.overview", "backbone.overview",
"jquery.browser", "jquery.browser",
......
...@@ -5,6 +5,7 @@ define("converse-dependencies", [ ...@@ -5,6 +5,7 @@ define("converse-dependencies", [
"strophe", "strophe",
"strophe.vcard", "strophe.vcard",
"strophe.disco", "strophe.disco",
"strophe.ping",
"backbone.browserStorage", "backbone.browserStorage",
"backbone.overview", "backbone.overview",
"jquery.browser", "jquery.browser",
......
...@@ -5,6 +5,7 @@ define("converse-dependencies", [ ...@@ -5,6 +5,7 @@ define("converse-dependencies", [
"strophe", "strophe",
"strophe.vcard", "strophe.vcard",
"strophe.disco", "strophe.disco",
"strophe.ping",
"bootstrapJS", // XXX: Can be removed, only for https://conversejs.org "bootstrapJS", // XXX: Can be removed, only for https://conversejs.org
"backbone.browserStorage", "backbone.browserStorage",
"backbone.overview", "backbone.overview",
......
...@@ -7,6 +7,7 @@ define("converse-dependencies", [ ...@@ -7,6 +7,7 @@ define("converse-dependencies", [
"strophe", "strophe",
"strophe.vcard", "strophe.vcard",
"strophe.disco", "strophe.disco",
"strophe.ping",
"bootstrapJS", // XXX: Only for https://conversejs.org "bootstrapJS", // XXX: Only for https://conversejs.org
"backbone.browserStorage", "backbone.browserStorage",
"backbone.overview", "backbone.overview",
......
/*
* Based on Ping Strophejs plugins (https://github.com/metajack/strophejs-plugins/tree/master/ping)
* This plugin is distributed under the terms of the MIT licence.
* Please see the LICENCE file for details.
*
* Copyright (c) Markus Kohlhase, 2010
* Refactored by Pavel Lang, 2011
*/
/**
* File: strophe.ping.js
* A Strophe plugin for XMPP Ping ( http://xmpp.org/extensions/xep-0199.html )
*/
/*
* AMD Support added by Thierry
*
*/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([
"strophe"
], function (Strophe) {
factory(
Strophe.Strophe,
Strophe.$build,
Strophe.$iq ,
Strophe.$msg,
Strophe.$pres
);
return Strophe;
});
} else {
// Browser globals
factory(
root.Strophe,
root.$build,
root.$iq ,
root.$msg,
root.$pres
);
}
}(this, function (Strophe, $build, $iq, $msg, $pres) {
Strophe.addConnectionPlugin('ping', {
_c: null,
// called by the Strophe.Connection constructor
init: function(conn)
{
this._c = conn;
Strophe.addNamespace('PING', "urn:xmpp:ping");
},
/**
* Function: ping
*
* Parameters:
* (String) to - The JID you want to ping
* (Function) success - Callback function on success
* (Function) error - Callback function on error
* (Integer) timeout - Timeout in milliseconds
*/
ping: function(jid, success, error, timeout)
{
var id = this._c.getUniqueId('ping');
var iq = $iq({type: 'get', to: jid, id: id}).c(
'ping', {xmlns: Strophe.NS.PING});
this._c.sendIQ(iq, success, error, timeout);
},
/**
* Function: pong
*
* Parameters:
* (Object) ping - The ping stanza from the server.
*/
pong: function(ping)
{
var from = ping.getAttribute('from');
var id = ping.getAttribute('id');
var iq = $iq({type: 'result', to: from,id: id});
this._c.sendIQ(iq);
},
/**
* Function: addPingHandler
*
* Parameters:
* (Function) handler - Ping handler
*
* Returns:
* A reference to the handler that can be used to remove it.
*/
addPingHandler: function(handler)
{
return this._c.addHandler(handler, Strophe.NS.PING, "iq", "get");
}
});
}));
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment