问题
A bit confused about EventSource
behavior.
It connects well and stays connected until it receives first message.
As i send a data: something\n\n
message, the connection is dropped and new one created.
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).
My server and client code are exactly as here: http://www.html5rocks.com/en/tutorials/eventsource/basics/
This happens on any browser.
回答1:
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
behaves strange on chunked Transfer-Encoding
You may remove Transfer-Encoding
header or set it to identity
.
If that does not help, make sure your web server does not set that header to chunked.
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).
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.
来源:https://stackoverflow.com/questions/13590755/eventsource-disconnects-after-first-message