【发布时间】:2011-11-09 23:11:44
【问题描述】:
我正在尝试在 php 聊天脚本中实现长轮询,但是长轮询使我发送的所有 ajax 请求进入睡眠状态等待原始请求。
顺便说一句,我正在使用 symfony 框架。
有什么想法吗?
-- 更新--
这是一些代码sn-ps
Javascript:
function whosTyping(person_id){
$.ajax({
type:'POST',
url:'/chat/whoisTyping',
data:'person_id='+person_id
dataType:'json',
success:function(resp){
if(resp == 'true') $('.is_typing').show();
else $('.is_typing').hide();
setTimeout(function(){
whosTyping(person_id)
},1000)
}
})
}
PHP:
public function executeWhoisTyping(sfWebRequest $request) {
$this->setLayout(false);
$this->setTemplate(false);
sfConfig::set('sf_web_debug', false);
$person_id = $request->getParameter('person_id');
$target_person_id = $this->getUser()->getGuardUser()->getPerson()->getId();
$check = Doctrine_Core::getTable('Typing')->findByPersonIdAndTargetPersonId($person_id)->toArray();
while(empty($check)){
usleep(1000);
clearstatcache();
$check = Doctrine_Core::getTable('Typing')->findByPersonIdAndTargetPersonId($person_id)->toArray();
}
Doctrine_Core::getTable('Typing')->createQuery()
->delete()
->where('target_person_id = ?', $target_person_id)
->execute();
return $this->renderText(json_encode('true'));
}
是的,我正在尝试发送常规 ajax 请求,但它们在等待长轮询响应时被取消”
【问题讨论】:
标签: jquery ajax long-polling