0
我試圖在一個php聊天腳本中實現長輪詢,但長輪詢將所有ajax請求發送到睡眠等待原始的請求。ajax和jquery和symfony(長輪詢問題)
順便說一句我正在使用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請求,但他們取消等待長期輪詢響應「