2011-11-09 55 views
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請求,但他們取消等待長期輪詢響應「

回答

1

這沒關係,大家好我想通了

事情是使其與symfony的工作,我不得不使用session_write_close()來結束當前會話

所以動作功能成爲follwing

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')); 
} 

希望可以幫到