长轮询(long-polling)

来源:互联网 发布:网络大电影投资合同 编辑:程序博客网 时间:2024/06/06 05:13

long-polling

<?
//设置当前脚本为无超时状态
set_time_limit(0);
//操作的文件名
$DATAFILE='dat.txt';
//保存当前时间
$time=time();
//用死循环持续检测文件
while(1){
  //判断文件存在
  file_exists($DATAFILE) and
  //判断文件修改
  filemtime($DATAFILE)>$time and
  //输出文件内容并断开HTTP
  die(file_get_contents($DATAFILE));
  //清空文件状态缓存
  clearstatcache();
  //等待1秒
  sleep(1);
};

?> 

 接着是客户端的代码     function polling(){

    $.post("http://www.test.cc/test.php",{},function(result){
        $("#clickme").html(result);
    });
    }
    window.onload = function (){
        polling();
        }
    
       
另一兄台博文:http://blog.csdn.net/chenjiebin/article/details/5675362

0 0
原创粉丝点击