首页 > 代码库 > ajax 轮询 和 php长连接

ajax 轮询 和 php长连接

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 = http://www.mamicode.com/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 = http://www.mamicode.com/count($newComments);
if($newCommentData != $OldCommentData){
if(!empty($comments)){

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

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

}

ajax 轮询 和 php长连接