码迷,mamicode.com
首页 > Web开发 > 详细

ajax 轮询 和 php长连接

时间:2017-06-26 15:36:32      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:tab   encode   时间   ajax   use   usleep   sleep   长连接   func   

php      部分

public  function get_comment(){
$post_id = I(‘get.post_id‘,0,‘intval‘);
$table = I(‘get.table‘);
$msg = I(‘get.msg‘);
$comment_model=D("Common/Comments");
$comments=$comment_model->where(array("post_table"=>$table,"post_id"=>$post_id,"status"=>1,‘user_type‘=>2))->order("createtime ASC")->select();

$OldCommentData = count($comments);

if($msg==‘init‘){
if(!empty($comments)){

$comments =self::forData($comments);
}
echo json_encode($comments);exit;
}
$time_count =0;
while(true){
set_time_limit(0);
$time_count++;
$newComments=$comment_model->where(array("post_table"=>$table,"post_id"=>$post_id,"status"=>1,‘user_type‘=>2))->order("createtime ASC")->select();

$newCommentData = count($newComments);
if($newCommentData != $OldCommentData){
if(!empty($comments)){

$newComments =self::forData($newComments);
echo json_encode($newComments);
break;
}

}
usleep(1000);
//一定的时间后没有数据变化也跳出
if($time_count >= 800){
$data = "";
echo json_encode($data);
break;
}
}

}

ajax 轮询 和 php长连接

标签:tab   encode   时间   ajax   use   usleep   sleep   长连接   func   

原文地址:http://www.cnblogs.com/fengshu/p/7080758.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!