接收服务器发送的事件通知

Receive Server-Sent Event Notifications

我有这个来自 w3schools 的服务器发送事件示例。 .php 代码工作正常。但是,我需要 .asp 版本,但该示例不起作用。我没有从服务器端脚本得到任何响应。有帮助吗?

     <!-----------Client side-------------->

     <!DOCTYPE html>
     <html>
     <body>

      <h1>Getting server updates</h1>
      <div id="result"></div>

      <script>
      if(typeof(EventSource) !== "undefined") {
      var source = new EventSource("demo_sse.asp");
      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>

     <!---------------Server side--------------->

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

你可能不相信,但是......试试这行:

Response.Write("data: The server time is: " & now()) & vbCrLf & vbCrLf

这对我有用! See this answer - the last one