Comet technology specifically implemented in conjunction with PHP and jquery

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.