简体   繁体   English

HTML5 服务器发送事件:技术细节?

[英]HTML5 Server sent events: technical details?

I'm interested in the technical background of HTML5's new server sent events.我对 HTML5 的新服务器发送事件的技术背景很感兴趣。 What really happens there?那里到底发生了什么?

  • you don't need a special feature on the server side - just send your data as always, eg html您不需要服务器端的特殊功能 - 只需像往常一样发送您的数据,例如 html
  • HTTP is still a request/response protocol. HTTP 仍然是一个请求/响应协议。 The new Content-type "text/event-stream" does not change that in my opinion在我看来,新的内容类型“文本/事件流”不会改变这一点

So is this concept just an encapsulated version of "plain old long polling "?那么这个概念只是“普通老式长轮询”的封装版本吗? On the other hand, it is often described as a persistent, bi-directional connection to the server.. that would be something different in my opinion.另一方面,它通常被描述为到服务器的持久、双向连接......在我看来这会有所不同。 I just want to unterstand how this can work on top of http.我只是想了解它是如何在 http 之上工作的。

It's more a codification of forever frame than long polling, relying on chunked transfer encoding rather than holding the connection open until data is ready.它更像是对永久帧的编码,而不是长轮询,它依赖于分块传输编码,而不是在数据准备好之前保持连接打开。 The data sent in the events is just text, though that text can, of course, be HTML, it's up to your app to do the appropriate stuff with it.在事件中发送的数据只是文本,尽管该文本当然可以是 HTML,但由您的应用程序对其进行适当的处​​理。 Items in the event stream look like this:事件流中的项目如下所示:

event: message
data: Any text data you want goes here

In the browser, when it receives this chunk, you see an event message on the EventSource which you can capture with the familiar addEventListener("message", callback) approach.在浏览器中,当它接收到这个块时,您会在EventSource上看到一条事件message ,您可以使用熟悉的addEventListener("message", callback)方法捕获该addEventListener("message", callback)

The main benefit over forever frame (or long polling) is a standardized interface (so basically, not worth updating existing working code for), the main advantage over Web Sockets is that it'll work just fine on cheap shared hosting where long running processes are not allowed.永久帧(或长轮询)的主要好处是标准化的接口(所以基本上,不值得更新现有的工作代码),与Web Sockets相比的主要优点是它可以在廉价的共享主机上正常运行,其中长时间运行的进程不允许。

PS The technical details are all in the spec PS 技术细节都在规范中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM