1

我有一个发送测试EventSource消息的服务器,如下所示:

要求:

GET /web/stream/status HTTP/1.1
Host: localhost:1010
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0
Accept: text/event-stream
Accept-Language: en-GB,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://localhost:1010/web/
Cookie: JSESSIONID=1miz08s4nu74q11sm7y44uwu2b
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

回复:

HTTP/1.1 200 OK
Content-Type: text/event-stream;charset=UTF-8
Connection: close
Server: Jetty(9.0.6.v20130930)

event: data
data: hello

所有行都以\r\n. 所以这对我来说是正确的,但如果我在 Firefox 中尝试这个......

var source = new EventSource('/web/stream/status');
source.onmessage = function(event) { console.log(event); };
source.onerror = function(event) { console.log(event); };

...然后它完全按照上面的方式连接并执行请求(实际上我将会话从 Wireshark 复制到 telnet 以对其进行测试),并根据 Wiresharkevent: data发送了内容,但既不调用onmessage也不onerror调用处理程序。onerror当我停止服务器时调用。

网络事物的响应选项卡中从未显示任何数据。

有谁知道出了什么问题?

4

1 回答 1

2

啊哈我找到了答案!Firefox 不喜欢;charset=UTF-8. 规范说你可以拥有;charset=utf-8Firefox 允许的。

对规范的解释相当严格,但足够公平。

此外,您只能获得onmessage()单行data:。如果 thedata:前面是 and event:name thenonmessage()则不被调用-相反,您必须使用它:

source.addEventListener('name_of_my_event', myEventHandler, false);
于 2015-08-06T07:37:36.533 回答