|
From: <ma...@us...> - 2011-03-06 12:09:32
|
Revision: 314
http://openautomation.svn.sourceforge.net/openautomation/?rev=314&view=rev
Author: mayerch
Date: 2011-03-06 12:09:26 +0000 (Sun, 06 Mar 2011)
Log Message:
-----------
New Feature: added a watchdog that makes sure that the connection stays fresh
This handles e.g. a browser on a system that goes to sleep and wakes up later. Or bad networks.
Modified Paths:
--------------
CometVisu/trunk/visu/lib/cometvisu-client.js
Modified: CometVisu/trunk/visu/lib/cometvisu-client.js
===================================================================
--- CometVisu/trunk/visu/lib/cometvisu-client.js 2011-03-05 14:44:58 UTC (rev 313)
+++ CometVisu/trunk/visu/lib/cometvisu-client.js 2011-03-06 12:09:26 UTC (rev 314)
@@ -20,6 +20,7 @@
*/
function CometVisu( urlPrefix )
{
+ var thisCometVisu = this;
this.urlPrefix = (null == urlPrefix) ? '' : urlPrefix; // the address of the service
this.addresses = []; // the subscribed addresses
this.filters = []; // the subscribed filters
@@ -28,6 +29,8 @@
this.device = ''; // the current device ID
this.running = false; // is the communication running at the moment?
this.xhr = false; // the ongoing AJAX request
+ this.watchdogTimer = 5; // in Seconds - the alive check intervall of the watchdog
+ this.maxConnectionAge = 60; // in Seconds - restart if last read is older
/**
* This function gets called once the communication is established and session information is available
@@ -55,6 +58,7 @@
if( this.running )
{ // retry initial request
this.xhr = $.ajax( {url:this.urlPrefix + 'r',dataType: 'json',context:this,data:this.buildRequest()+'&t=0', success:this.handleRead ,error:this.handleError/*,complete:this.handleComplete*/ } );
+ watchdog.ping();
}
return;
}
@@ -66,6 +70,7 @@
if( this.running )
{ // keep the requests going
this.xhr = $.ajax( {url:this.urlPrefix + 'r',dataType: 'json',context:this,data:this.buildRequest()+'&i='+lastIndex, success:this.handleRead ,error:this.handleError/*,complete:this.handleComplete*/ } );
+ watchdog.ping();
}
};
@@ -149,6 +154,34 @@
var request = 'a=' + address + '&v=' + value;
$.ajax( {url:this.urlPrefix + 'w',dataType: 'json',context:this,data:request} );
}
+
+ /**
+ * Restart the read request, e.g. when the watchdog kicks in
+ */
+ this.restart = function()
+ {
+ if( this.xhr.abort ) this.xhr.abort();
+ this.handleRead(); // restart
+ }
+
+ /**
+ * The watchdog to recreate a read request when it stopped somehow
+ */
+ var watchdog = (function(){
+ var last = new Date();
+ var aliveCheckFunction = function(){
+ var now = new Date();
+ if( now - last < thisCometVisu.maxConnectionAge * 1000 ) return;
+ thisCometVisu.restart();
+ last = now;
+ };
+ var aliveHandler = setInterval( aliveCheckFunction, thisCometVisu.watchdogTimer * 1000 );
+ return {
+ ping: function(){
+ last = new Date();
+ }
+ };
+ })();
};
CometVisu.prototype.update = function( json ) {}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|