2011-11-09 13 views
0

私はPHPのチャットスクリプトで長いポーリングを実装しようとしていますが、長いポーリングは元のものを待っているスリープ状態にするすべてのajaxリクエストを入れます。ajaxとjqueryとsymfony(ロングポーリングの問題)

私はsymfonyフレームワークで作業しています。

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を送信しようとしている

- - UPDATE

ここではいくつかのコードが

Javascriptをスニペットです長いポーリング応答を待ってキャンセルされます」

答えて

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')); 
} 
follwingなって大丈夫です

希望すること