具体看代码,费话不说

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请求,又没有降低实时性。

COMET技术具体实现 结合PHP和JQUERY的更多相关文章

  1. 基于 Asp.Net的 Comet 技术解析

    Comet技术原理 来自维基百科:Comet是一种用于web的技术,能使服务器能实时地将更新的信息传送到客户端,而无须客户端发出请求,目前有两种实现方式,长轮询和iframe流. 简单的说是一种基于现 ...

  2. Comet技术详解:基于HTTP长连接的Web端实时通信技术

    前言 一般来说,Web端即时通讯技术因受限于浏览器的设计限制,一直以来实现起来并不容易,主流的Web端即时通讯方案大致有4种:传统Ajax短轮询.Comet技术.WebSocket技术.SSE(Ser ...

  3. 初识Comet技术

    一.服务器推送技术概述 服务器推送技术(Servers Push)的基础思想是将浏览器主动查询信息改为服务器主动发送信息.服务器发送一批数据,浏览器显示这些数据,同时保证与服务器的连接.当服务器需要再 ...

  4. Comet技术在Java Web中的应用

    HTTP协议是互联网上大量信息交换的基础,其特点是,它是基于请求-响应模式的无状态的单向协议,即,必须由客户端发起一个请求建立连接,服务器接收请 求,把数据返回给客户端,然后释放连接.下一次,再由客户 ...

  5. 现在学习 JavaScript 的哪种技术更好:Angular、jQuery 还是 Node.js?(转)

    本文选自<开发者头条>1 月 7 日最受欢迎文章 Top 3,感谢作者 @WEB资源网 分享. 欢迎分享:http://toutiao.io/contribute 这是一个发布在 Quor ...

  6. Comet技术

    1.Comet是什么? 维基百科: Comet是一种用于web的推送技术,能使服务器实时地将更新的信息传送到客户端,而无须客户端发出请求,目前有两种实现方式,长轮询和iframe流. 说白了就是web ...

  7. Comet技术浅论

    1.如何实现一个轮询? function getMessage(url,callback){ var XHR=new XMLHttpRequest(); XHR.open('get',url,true ...

  8. 前后台交互经常使用的技术汇总(后台:Java技术,前台:Js或者Jquery)

    1:由于针对特定的前后台交互用到的知识总结,所以不大量贴代码,主要给出思路,方便自己以后脑补和技术总结,当然也希望可以帮助到别人. 后台Json和其他格式转化,之前总结过Json和对象,集合,字符串的 ...

  9. Comet:基于 HTTP 长连接的“服务器推”技术解析

    原文链接:http://www.cnblogs.com/deepleo/p/Comet.html 一.背景介绍 传统web请求,是显式的向服务器发送http Request,拿到Response后显示 ...

随机推荐

  1. (转)Server Tomcat v6.0 Server at localhost was unable to start within 45 seconds

    仰天长啸 Server Tomcat v6.0 Server at localhost was unable to start within 45 seconds... 当启动tomcat时候出现 S ...

  2. 转载 Eclipse下的SSH开发例子

    前言 确实好久没有写过了,一直以来把写博文当作自己学习的总结,当作做过的笔记,随时都可以拿出来看看.不过最近习惯了用OneNote和印象笔记,所以就很少在论坛写博文.但是偶尔看到几篇被转载了,也小小的 ...

  3. html 调用 activeX(c++)

    1.新建MFC ActiveX 2.添加方法 3.找到add函数编写代码 4.在test.idl中找到最后一个uuid 5.编译工程,会自动注册控件 6.html中的代码 <html> & ...

  4. navicat for mysql 将结果导出到txt文件

    其实navicat 是带这个导出功能的,可是很多人不知道怎么用. 1.如图:查询sql语句.得到结果

  5. 如何让secureCRT显示Linux的颜色

    style="padding-bottom: 0px; line-height: 1.5; margin: 0px; padding-left: 0px; padding-right: 0p ...

  6. 认识JSONP

    jsonp 全称是JSON with Padding 我们大家都知道ajax是不能发起跨域请求,现在可以通过jsonp来弥补ajax的这一缺陷 通过script标签的src属性就可以实现跨域请求.如( ...

  7. JQuery动态表格

    功能实现:点击添加按钮,表格增加一行并给其name属性赋予的值,方便获取 点击删除,自动删除这一行 JQuery中定义一个count变量 var count = 1; function add() { ...

  8. H5 progress标记

    进度条标记 示例:<progress class="processbar" id="processbar" max="100" val ...

  9. plsql基本语法(

    1. 定义常量的语法格式    常量名 constant 类型标识符 [not null]:=值;    常量,包括后面的变量名都必须以字母开头,不能有空格,不能超过30个字符长度,同时不能和保留字同 ...

  10. python核心编程-第四章-个人笔记

    1.所有的python对象都拥有三个特性: ①身份:每个对象都有唯一的身份标识自己,可用内建函数id()来得到.基本不会用到,不用太关心 >>> a = 2 >>> ...