COMET技術具體實現 結合PHP和JQUERY

來源:互聯網
上載者:User

標籤:style   blog   color   資料   io   cti   

具體看代碼,費話不說

PHP服務端

$mem = new RTMEM();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));

 

以下是JS端

//comet ajax////var Comet = 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 seconds                        setTimeout(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 } );         }    }}///////    var comet = new Comet({        url:‘binsrv/rt_alm.php?type=getrtalm‘,        callback:function(json){ //接收到資料的處理            if(typeof(page)!="undefined" && typeof(page.processAlmData)=="function")                page.processAlmData(json);                    }            });             comet.connect();    

這樣的話,伺服器查詢資料有更新才會返回AJAX請求,沒有更新會直到逾時(PHP預設30秒逾時),這時COMET會重新串連

這樣大大降低了頻繁的AJAX請求,又沒有降低即時性。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.