• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ajax轮询和php长连接

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 只看加粗的字体

js   部分

      1:  ajax 成功回调函数中 一定要用时间函数间隔调用  get_comment().

get_comments('init');
function get_comments(msg){
if(msg == undefined){
msg = '';
}
var post_id ="{$id}";
$.ajax({
type: "GET",
url: "",
data: {},
dataType: "json",
success: function(data){
// 这里 你自己处理 自己的data.
setTimeout("get_comments()",60000);
}
})

}

php部分

      1:   根据条件,取得数据的条数,取名 old_counts

      2:  根据 while(true)循环,再次查询数据库的条数,new_counts;

      3: 新的条数和旧地条数做比较。比较结果不一样,返回结果。

      4:  break ,跳出循环

      5 : $time_count 是  对于一定的时间内,数据没有更新,也跳出循环。

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);// 函数允许脚本运行的时间。设为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;
}
}

}

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
php5.2与5.3性能测试对比发布时间:2022-07-10
下一篇:
PHP怎么调用其他类的方法发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap