如何处理HTML5中的服务器发送事件?

要在HTML5中处理服务器发送的事件,您可以尝试运行以下代码:

<!DOCTYPE HTML>
<html>
   <head>
      <script>
         document.getElementsByTagName("eventsource")[0].addEventListener("server-time", eventHandler, false);
         function eventHandler(event)
         {
            //服务器发送的警报时间
            document.querySelector('#ticker').innerHTML = event.data;
         }
      </script>
   </head>
   <body>
      <div id = "sse">
         <eventsource src = "/cgi-bin/ticker.cgi" />
      </div>
      <div id = "ticker" name = "ticker">
         [TIME]
      </div>
   </body>
</html>