高手来帮我分析一下,头大了。。

#1 qiudong26

写了一个简单的ajax long-polling在线聊天的demo,采用jQuery+mysql+php
数据表如下
1.jpg
以下为firebug截图
$.get获取最新内容
$.post为发送消息
2.jpg
遇到的问题是,当我$.post新的消息时,每次要等$.get完了才能post过去,哪怕用另外的页面submit,也要等服务器处理完成,才能插入新的消息到数据库。不知道我的代码哪里出问题了。代码如下。顺便把源代码和数据库附上。
class main extends spController
{
        function index(){//显示页面
                $chat=spClass('chat');
                $result=$chat->findAll();
                $this->result=$result;
                $this->display(APP_PATH.'/view/index.html');
        }
        function getNew(){//长轮询获取新数据
                set_time_limit(0);
                $chat=spClass('chat');
                $time=time();
                $last=$this->spArgs("last");
                while(time()-$time<30){
                        $sql="select content from chat where id>".$last." order by id desc limit 1";
                        $result=$chat->findSql($sql);
                        if($result){
                                echo $result[0]['content'];
                                break;
                        }
                        else{
                                sleep(1);
                        }
                }
                if($result[0]['content']=='') echo 'timeout';
        }
        function insertNew(){
                $content=$this->spArgs('content');
                $timer=time();
                $newrow=array('content'=>$content,'timer'=>$timer);
                $chat=spClass('chat');
                $chat->create($newrow);
        }
}

2010-10-31 12:48:03

#2 jake

getNew()由于set_time_limit(0);
所以PHP程序会执行完毕后,才会释放资源给另一个程序。

2010-11-01 09:06:19

#3 qiudong26

去掉set_time_limit(0)还是不行,不知道为啥。

2010-11-01 10:24:44

#4 jake

去掉set_time_limit(0)还是不行,不知道为啥。
qiudong26 发表于 2010-11-1 10:24
改变了一下那种while不断循环查询的获取方式吧,从MYSQL这端做这种长时间的查询并不是很好。一般来说,这种情况最好用memcached

2010-11-01 11:58:28