具体看代码,费话不说

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. jsp include 乱码问题的解决

    jsp include 乱码问题的解决 博客分类: Java JSPWeb浏览器IESpring  jsp include 乱码问题的解决 jsp include 的文件有时候会出现乱码,经过测试发现 ...

  2. HDU 3306 - Another kind of Fibonacci

    给你 A(0) = 1 , A(1) = 1 , A(N) = X * A(N - 1) + Y * A(N - 2) (N >= 2). 求 S(N) = A(0) 2 +A(1) 2+……+ ...

  3. 关于dwt文件和lbi文件

    1,dwt 文件是网页模板文件(Dreamweaver Template), 在创建网站的多个网页的时候,通常可以将网页的共同部分创建成为一个模板, 然后给多个网页调用, 以实现网页代码的重复利用. ...

  4. RemoteViews的理解和使用

    一.RemoteViews简介 作用:跨进程更新界面                         使用场景:通知栏.桌面小部件 二.在通知栏上的应用 原理:通过RemoteViews加载布局,通过 ...

  5. flask twisted 结合方案

    from flask import Flask, render_template, g app = Flask(__name__) @app.route("/") def inde ...

  6. 过河问题--nyoj题目47

    过河问题 时间限制:1000 ms  |  内存限制:65535 KB 难度:5   描述 在漆黑的夜里,N位旅行者来到了一座狭窄而且没有护栏的桥边.如果不借助手电筒的话,大家是无论如何也不敢过桥去的 ...

  7. C#代码 json类

    using System; using System.Collections.Generic; using System.Collections; using System.Text; using S ...

  8. poI地址纠偏查询存储过程理解

    1.POI存储过程查询 服务器类型:PostgreSQL function name 函数名字 address_parse_no_save(lon float8, lat float8) DECLAR ...

  9. OC中如何把字典中的数据拼接成url字符串

    在使用objective-c语言开发iOS应用中,会向服务器通过URL请求一些数据,因此对URL的拼接肯定少不了.而在iOS中,我们一般是通过将字典中的数据拼接成我们要请求的URL字符串,那这个是怎么 ...

  10. STM32F407VG (三)ADC

    12位ADC是一种逐次逼近型模拟数字转换器. 它有多达19个通道,可測量16个外部和2个内部信号源和VBAT通道.各通道的A/D转换能够单次.连续.扫描或间断模式运行. ADC的结果能够左对齐或右对齐 ...