Specific look at the code, crap don't say
PHP Service Side
$mem=NewRtmem ();if(!$mem-Conn ())Exit(' No MEM server ');if(!$mem-getstate ())Exit(' Moonjksrv is not runing ');$alminfo=$mem->get (' Alm_info ');if(!$alminfo)Exit(' No alarm ');$almobj= Json_decode ($alminfo);if(!$almobj)Exit(' No JSON data ');$lastmodif=isset($_get[' timestamp ']) ?$_get[' timestamp ']: 0;$currentmodif=$almobj-timestamp; while($currentmodif<=$lastmodif)//Check if the data file has been modified{Usleep(1000000);//Sleep 1 Second to unload the CPU Clearstatcache(); $alminfo=$mem->get (' Alm_info '); if(!$alminfo)Exit(' No alarm '); $almobj= Json_decode ($alminfo); if(!$almobj)Exit(' No JSON data '); $currentmodif=$almobj-timestamp;}Exit(Json_encode ($almobj));
The following is the JS end
//Comet ajax////varComet =function(options) { This. Init (options);}; Comet.prototype={constructor:comet, init:function(options) { This. options ={URL:"", Callback:function(){} } This. options = $.extend ( This. options,options| |{}); This. url = This. Options.url; This. Callback = This. Options.callback; This. Timestamp = 0; This. NoError =true; This. Lock =true; }, connect:function(){ This. Lock =false; This. Ajaxloop (); }, Disconnect:function(){ This. Lock =true; }, Ajaxloop:function(){ if( This. URL &&! This. Lock) { var_this = This; $.ajax ({url:_this.url, type:' Get ', Data:' timestamp= ' +_this.timestamp, DataType:' JSON ', Cache:false, Success:function(JSON) {_this.timestamp= json[' timestamp ']; _this.handleresponse (JSON); _this.noerror=true; }, Complete:function(){ if(_this.noerror) {_this.ajaxloop (); }Else{ //If a connection problem occurs, try to reconnect each 1 secondsSetTimeout (function() {_this.ajaxloop ()}, 1000); } _this.noerror=false; }})}, Handleresponse:function(response) { This. Callback (response); }, DoRequest:function(Request) {if( This. URL &&! This. Lock) {$.get ( This. URL, {' msg ': request}); } }}/////// varComet =NewComet ({URL:' Binsrv/rt_alm.php?type=getrtalm ', Callback:function(JSON) {//processing of incoming data if(typeof(page)! = "undefined" &&typeof(page.processalmdata) = = "function") Page.processalmdata (JSON); } }); Comet.connect ();
In this case, the server query data will be updated to return the AJAX request, no updates until the timeout (php default 30 seconds timeout), then comet will reconnect
This greatly reduces the frequent AJAX requests without reducing the real-time nature.