第一条消息后,EventSource断开连接 [英] EventSource disconnects after first message
问题描述
对于 EventSource
行为有些困惑.
它连接良好并保持连接状态,直到收到第一条消息为止.
It connects well and stays connected until it receives first message.
当我发送 data:something \ n \ n
消息时,连接被断开并创建了新的连接.
As i send a data: something\n\n
message, the connection is dropped and new one created.
使用 Content-Type:文本/事件流
标头和连接被识别为事件流,并且检查器显示待处理
状态.
Using Content-Type: text/event-stream
header and the connection are recognized as a event stream and the inspector shows pending
status.
正如我所说,一切正常,直到我发送第一条消息为止.
As i said, everything works well until i send first message.
此后,连接中断并创建了新连接,并且消息未发送到浏览器(至少页面未按应有的状态更新).
After this the connection are interrupted and new one created and message not sent to browser(at least the page not updated as it should).
我的服务器和客户端代码与此处完全相同: http://www.html5rocks.com/en/tutorials/eventsource/basics/
My server and client code are exactly as here: http://www.html5rocks.com/en/tutorials/eventsource/basics/
在任何浏览器上都会发生这种情况.
This happens on any browser.
推荐答案
确保您的服务器代码未发送 Transfer-Encoding
标头.
Make sure your server code are not sending Transfer-Encoding
header.
或者,如果发送它,请确保未将其设置为分块.
Or if it sending it, make sure it is not set to chunked.
EventSource
在分块的 Transfer-Encoding
EventSource
behaves strange on chunked Transfer-Encoding
您可以删除 Transfer-Encoding
标头,或将其设置为 identity
.
You may remove Transfer-Encoding
header or set it to identity
.
如果这样做没有帮助,请确保您的Web服务器未将标头设置为分块.
If that does not help, make sure your web server does not set that header to chunked.
如果这样做,并且您无法更新其配置,请确保您的代码将标头设置为 identity
(由代码设置的标头将覆盖由Web服务器设置的标头).
If it does and you can not update its config, make sure your code are setting the header to identity
(headers set by your code will override ones set by web server).
如果仅删除代码中的标头,则浏览器将使用一个由Web服务器设置的标头,
因此请确保将其设置为 identity
,而不是将其删除.
If you simply remove the header in your code, the browser will use one set by web server,
so make sure to set it to identity
rather than remove.
这篇关于第一条消息后,EventSource断开连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!