我正在尝试在php聊天脚本中实现长轮询,但是长轮询会将我发送的所有ajax请求放入休眠状态等待原始状态。
BTW我正在使用symfony框架。
有什么想法吗?
- 更新 -
以下是一些代码段
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请求,但是它们会被取消,等待长轮询响应“
答案 0 :(得分:1)
没关系,我想出来了
事情是让它与symfony一起工作我必须使用session_write_close()来结束当前会话
所以动作功能成为了以下
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,$target_person_id)->toArray();
while(empty($check)){
usleep(100000);
clearstatcache();
session_write_close();
$check = Doctrine_Core::getTable('Typing')->findByPersonIdAndTargetPersonId($person_id,$target_person_id)->toArray();
}
return $this->renderText(json_encode(!empty($check) ? 'true' : 'false'));
}
希望有所帮助