Procházet zdrojové kódy

XEP-0199: XMPP Ping

Use modified StropheJS ping plugin (AMD support)
Add Ping functionality
Add Pong handler

Fix to issue #144:
thierrytiti před 10 roky
rodič
revize
fb589398ad

+ 53 - 0
converse.js

@@ -276,6 +276,7 @@
             keepalive: false,
             message_carbons: false,
             no_trimming: false, // Set to true for phantomjs tests (where browser apparently has no width)
+            ping_interval: 180, //in seconds
             play_sounds: false,
             sounds_path: '/sounds/',
             password: undefined,
@@ -663,10 +664,57 @@
             },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 () {
             // We need to re-register all the event handlers on the newly
             // created connection.
             this.initStatus($.proxy(function () {
+                this.registerPongHandler();
+                this.registerPingHandler();
                 this.rosterview.registerRosterXHandler();
                 this.rosterview.registerPresenceHandler();
                 this.chatboxes.registerMessageHandler();
@@ -713,6 +761,8 @@
             this.enableCarbons();
             this.initStatus($.proxy(function () {
 
+                this.registerPingHandler();
+                this.registerPongHandler();
                 this.chatboxes.onConnected();
                 this.giveFeedback(__('Contacts'));
                 if (this.callback) {
@@ -5828,6 +5878,9 @@
         'send': function (stanza) {
             converse.connection.send(stanza);
         },
+        'ping': function (jid) {
+            converse.ping(jid);
+        },
         'plugins': {
             'add': function (name, callback) {
                 converse.plugins[name] = callback;

+ 1 - 0
docs/CHANGES.rst

@@ -16,6 +16,7 @@ Changelog
 * I18N: Autodetection of User Locale if no i18n setting is set. [thierrytiti]
 * CSS: Fonts Path: editabable $font-path via sass/variables.scss [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)
 ------------------

+ 11 - 0
docs/source/configuration.rst

@@ -368,6 +368,17 @@ Specify the locale/language. The language must be in the ``locales`` object. Ref
 
 .. _`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
 -----------
 

+ 3 - 1
main.js

@@ -38,6 +38,7 @@ require.config({
         "strophe.disco":            "components/strophejs-plugins/disco/strophe.disco",
         "strophe.roster":           "src/strophe.roster",
         "strophe.vcard":            "src/strophe.vcard",
+        "strophe.ping":             "src/strophe.ping",
         "text":                     'components/requirejs-text/text',
         "tpl":                      'components/requirejs-tpl-jcbrand/tpl',
         "typeahead":                "components/typeahead.js/index",
@@ -170,7 +171,8 @@ require.config({
         'strophe.disco':        { deps: ['strophe'] },
         'strophe.register':     { deps: ['strophe'] },
         'strophe.roster':       { deps: ['strophe'] },
-        'strophe.vcard':        { deps: ['strophe'] }
+        'strophe.vcard':        { deps: ['strophe'] },
+        'strophe.ping':         { deps: ['strophe'] }
     }
 });
 

+ 1 - 0
non_amd.html

@@ -35,6 +35,7 @@
     <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/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/backbone//backbone.js"></script>
     <script type="text/javascript" src="components/backbone.browserStorage/backbone.browserStorage.js"></script>

+ 1 - 0
src/deps-full.js

@@ -6,6 +6,7 @@ define("converse-dependencies", [
     "strophe",
     "strophe.vcard",
     "strophe.disco",
+    "strophe.ping",
     "backbone.browserStorage",
     "backbone.overview",
     "jquery.browser",

+ 1 - 0
src/deps-no-otr.js

@@ -5,6 +5,7 @@ define("converse-dependencies", [
     "strophe",
     "strophe.vcard",
     "strophe.disco",
+    "strophe.ping",
     "backbone.browserStorage",
     "backbone.overview",
     "jquery.browser",

+ 1 - 0
src/deps-website-no-otr.js

@@ -5,6 +5,7 @@ define("converse-dependencies", [
     "strophe",
     "strophe.vcard",
     "strophe.disco",
+    "strophe.ping",
     "bootstrapJS", // XXX: Can be removed, only for https://conversejs.org
     "backbone.browserStorage",
     "backbone.overview",

+ 1 - 0
src/deps-website.js

@@ -7,6 +7,7 @@ define("converse-dependencies", [
     "strophe",
     "strophe.vcard",
     "strophe.disco",
+    "strophe.ping",
     "bootstrapJS", // XXX: Only for https://conversejs.org
     "backbone.browserStorage",
     "backbone.overview",

+ 100 - 0
src/strophe.ping.js

@@ -0,0 +1,100 @@
+/*
+* 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");
+        }
+});
+
+}));