H5接收服务器的推送(Server-Sent)

  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";

新闻标题:H5接收服务器的推送(Server-Sent)
标题路径:https://www.cdcxhl.com/article40/jgioho.html

成都网站建设公司_创新互联,为您提供网站策划定制网站外贸建站微信公众号响应式网站面包屑导航

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

h5响应式网站建设