• HTML5学习(十一)---服务器发送事件


    参考教程:http://www.w3school.com.cn/html5/html_5_serversentevents.asp

    HTML5 服务器发送事件(server-sent event)允许网页获得来自服务器的更新。

    Server-Sent 事件 - 单向消息传递

    Server-Sent 事件指的是网页自动获取来自服务器的更新。

    以前也可能做到这一点,前提是网页不得不询问是否有可用的更新。通过服务器发送事件,更新能够自动到达。

    例子:Facebook/Twitter 更新、估价更新、新的博文、赛事结果等。

    接收 Server-Sent 事件通知

    EventSource 对象用于接收服务器发送事件通知:

    <!DOCTYPE html>
    <html>
    <body>
    <h1>获得服务器更新</h1>
    <div id="result"></div>
    
    <script>
    if(typeof(EventSource)!=="undefined")
      {
      var source=new EventSource("/example/html5/demo_sse.php");
      source.onmessage=function(event)
        {
        document.getElementById("result").innerHTML+=event.data + "<br />";
        };
      }
    else
      {
      document.getElementById("result").innerHTML="Sorry, your browser does not support server-sent events...";
      }
    </script>
    
    </body>
    </html>

    例子解释:

    • 创建一个新的 EventSource 对象,然后规定发送更新的页面的 URL(本例中是 "demo_sse.php")
    • 每接收到一次更新,就会发生 onmessage 事件
    • 当 onmessage 事件发生时,把已接收的数据推入 id 为 "result" 的元素中

    检测 Server-Sent 事件支持

    在上面的 TIY 实例中,我们编写了一段额外的代码来检测服务器发送事件的浏览器支持情况:

    if(typeof(EventSource)!=="undefined")
      {
      // Yes! Server-sent events support!
      // Some code.....
      }
    else
      {
      // Sorry! No server-sent events support..
      }
    

    服务器端代码实例

    为了让上面的例子可以运行,您还需要能够发送数据更新的服务器(比如 PHP 和 ASP)。

    服务器端事件流的语法是非常简单的。把 "Content-Type" 报头设置为 "text/event-stream"。现在,您可以开始发送事件流了。

    PHP 代码 (demo_sse.php):

    <?php
    header('Content-Type: text/event-stream');
    header('Cache-Control: no-cache');
    
    $time = date('r');
    echo "data: The server time is: {$time}
    
    ";
    flush();
    ?>
    

    ASP 代码 (VB) (demo_sse.asp):

    <%
    Response.ContentType="text/event-stream"
    Response.Expires=-1
    Response.Write("data: " & now())
    Response.Flush()
    %>
    

    代码解释:

    • 把报头 "Content-Type" 设置为 "text/event-stream"
    • 规定不对页面进行缓存
    • 输出发送日期(始终以 "data: " 开头)
    • 向网页刷新输出数据

    EventSource 对象

    在上面的例子中,我们使用 onmessage 事件来获取消息。不过还可以使用其他事件:

    事件描述
    onopen 当通往服务器的连接被打开
    onmessage 当接收到消息
    onerror 当错误发生
  • 相关阅读:
    JDBC
    Maven入门初级教程
    os.path路径拓展 python3
    requests实现文件下载, 期间显示文件信息&下载进度_python3
    yield浅析-Python3
    Scoop
    U盘启动盘制作工具(安装Linux)
    JavaScript摘要笔记
    Hexo+Github搭建博客&各种设置
    Linux下搭建svn服务端
  • 原文地址:https://www.cnblogs.com/liuzenglong/p/3140881.html
Copyright © 2020-2023  润新知