ajax 轮询 和 php长连接

2021-07-12 23:07

阅读:645

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;
}
}

}


评论


亲,登录后才可以留言!