#1 qiudong26
写了一个简单的ajax long-polling在线聊天的demo,采用jQuery+mysql+php数据表如下

以下为firebug截图
$.get获取最新内容
$.post为发送消息

遇到的问题是,当我$.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