1. js代码:
    <script>
    if(typeof(EventSource)!=="undefined"){
    var source=new EventSource("test.php");
    source.onmessage=function(event)
    {
      console.log(event.data)
    };
    }else{
    console.log('浏览器不支持Server-Sent')
    }
    </script>
  2. php代码:
    <?php
    header('Content-Type: text/event-stream');
    $arr = array(
        'name'=>'lee'
    );
    $data = json_encode($arr);
    echo "data: {$data}\n\n";