服务器在推送数据上发送事件延迟

时间:2014-01-16 01:53:30

标签: server-sent-events

您好我正在使用SSE,我在将通知推送到浏览器时遇到了一些问题。我在我的localhost中测试它并且工作正常但是当我在实时网站上测试它时需要至少5分钟将通知推送到浏览器。我已经设定了睡眠(2);有人可以帮帮我吗?谢谢!

以下是我的代码:)

服务器端

public function start(){

 header("Content-Type: text/event-stream");
 header('Cache-Control: no-cache');
 header('Connection: keep-alive');

$login_id = $this->session->userdata('login_id');
$start = time();
$keep_time_alive = 300;
echo "retry: 2000\n";

while (true) {
              $session_follower = $this->session->userdata('follower');
              $session_following = $this->session->userdata('following');
              $session_notification = $this->session->userdata('notification');
              $session_message = $this->session->userdata('message');
              $session_viewed_profile_id = $this->session->userdata('viewed_profile_id');
              $session_viewed_profile_type = $this->session->userdata('viewed_profile_type');
              $echo = $this->check($login_id,$session_follower,$session_notification,$session_following,$session_viewed_profile_id,$session_viewed_profile_type,$session_message);

               if($echo != ""){
                    echo "id: ".$this->id++."\n";
                    echo $echo;
                    ob_flush();
                    flush();            
                }else{
                    $send_comment = $this->send_comment($start,$keep_time_alive);
                    if($send_comment == 0){
                        echo ': '.sha1(mt_rand())."\n\n";
                    }

                    ob_flush();
                    flush();
                    //continue;
                }                       

               sleep(2);
            }
}

客户代码:

if(typeof(EventSource)!=="undefined"){
  var source=new EventSource("<?php echo base_url(); ?>server_events/start/");


                    source.addEventListener("notification", function(event){            
                                if(event.data != 0){$('#dd-notinotext').css("opacity",1); $('#dd-notinotext').html(event.data);}else{$('#dd-notinotext').css("opacity",0);}
                                if($( "#dd" ).hasClass( "active")){ get_details('#dd');}
                    }, false);



                    source.addEventListener("follower", function(event){    
                                if(event.data != 0){$('#dd2-notinotext').css("opacity",1); $('#dd2-notinotext').html(event.data);}else{$('#dd2-notinotext').css("opacity",0);}
                                if($( "#dd2" ).hasClass( "active")){get_details('#dd2');}
                                if(page_viewed == 'followers'){
                                        var val = $('#condition').val();
                                        $('#list_contents').load("<?php echo base_url(); ?>followers/page/true/"+ val);
                                }
                    }, false);

                    source.addEventListener("following", function(event){           
                                if(page_viewed == 'following'){
                                    var val = $('#condition').val();
                                    var profile_type = $('#condition_type').val();
                                    $('#list_contents').load("<?php echo base_url(); ?>following/page/true/"+profile_type+'/' + val);
                                }
                    }, false);

                    source.addEventListener("message", function(event){         
                                if(event.data != 0){$('#dd4-notinotext').css("opacity",1); $('#dd4-notinotext').html(event.data);}else{$('#dd4-notinotext').css("opacity",0);}
                                if($( "#dd4" ).hasClass( "active")){ get_details('#dd4');}
                    }, false);




        }else{
                    alert('browser does not support SSE!');
        }   

备注: *在服务器端,我只是检查数据库中是否有任何移动,如果我只是将行计数FLUSH到我的客户端。我没有发布我的所有服务器端代码,因为它太长但是功能检查只是为了检查我桌子上的行数。

  • 请告诉我,如果我正在使用SSE,如果我错了请告诉我如何改进我的代码我还是SSE.THANKS的新手! :)

0 个答案:

没有答案