EventSource

  1. php代码

     # index.php
     <?php
     header('Cache-Control: no-cache');
     header('Content-Type: text/event-stream');
     $time = date('Y-m-d H:i:s');
     echo "retry:1000\ndata: {$time}\n\n";
     flush();
    
  2. 对应js代码

     var source=new EventSource("http://localhost/index.php");
     source.onmessage=function(event)
     {
         console.log(event.data);
     };
    

results matching ""

    No results matching ""