1. 程式人生 > >ajax 輪詢 和 php長連接

ajax 輪詢 和 php長連接

create model usleep enc mon 沒有 for intval 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長連接