【问题标题】:ajax and jquery and symfony (Long polling issue)ajax and jquery and symfony (Long polling issue)
【发布时间】: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


    【解决方案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'));
    }
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2019-03-24
      • 1970-01-01
      • 1970-01-01
      • 2015-01-07
      • 2018-09-27
      • 1970-01-01
      • 2022-12-01
      • 1970-01-01
      • 2010-12-31
      相关资源
      最近更新 更多