Jersey Server-Sent 事件不适用于 Firefox
Jersey Server-Sent Events not working with Firefox
泽西岛 2.1.4,Java8,Tomcat8,Firefox 38.0.1
服务器:
@GET
@Produces(SseFeature.SERVER_SENT_EVENTS)
public EventOutput listenToBroadcast() {
final EventOutput eventOutput = new EventOutput();
this.broadcaster.add(eventOutput);
return eventOutput;
}
客户:
var source = new EventSource('broadcast');
source.addEventListener('event', function(event) {
alert('event');
}, false);
source.onopen = function() {
alert('connection open');
};
使用 Firefox 时,连接打开警报不会在页面加载时显示。
Firefox 在控制台中显示以下错误:Firefox 无法与位于 http://localhost:8080/broadcast 的服务器建立连接。
onopen 函数确实在第一个事件进入时被调用。在这种情况下,只有 onopen 函数被调用,而不是事件侦听器。
Chrome 工作正常。此外,此 demo 可与 Firefox 正常配合使用。
在页面加载时,在服务器发送事件之前,Firefox 中的“网络”选项卡显示它收到了 /broadcast SSE 端点的 OK 200,但不存在 headers。 Jersey 日志显示以下连接建立:
o.glassfish.jersey.filter.LoggingFilter : 11 * Server has received a request on thread http-nio-8080-exec-3
11 > GET http://localhost:8080/broadcast
11 > accept: text/event-stream
11 > accept-encoding: gzip, deflate
11 > accept-language: en-US,en;q=0.5
11 > cache-control: no-cache
11 > connection: keep-alive
11 > host: localhost:8080
11 > pragma: no-cache
11 > referer: http://localhost:8080/test_sse.html
11 > user-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0
11 * Server responded with a response on thread http-nio-8080-exec-3
11 < 200
11 < Content-Type: text/event-stream
我的客户端在创建 EventSource 连接后等待 EventSource.onOpen
事件 (new EventSource()
)。 Chrome 在连接打开后立即调用 onOpen
回调,但 Firefox 只会在服务器发送第一个事件时调用它。为了解决这个问题,我在服务器打开 SSE 连接后立即发送评论事件。 Firefox 获取此事件,这是无意义的,并调用 onOpen
函数。
这是我的服务器端客户端订阅代码:
@GET
@Produces(SseFeature.SERVER_SENT_EVENTS)
public EventOutput listenToBroadcast() {
final EventOutput eventOutput = new EventOutput();
this.broadcaster.add(eventOutput);
// firefox doesn't call the EventSource.onOpen callback when the connection is created, but it requires at least one event to be sent, so a
// meaningless comment event is used
OutboundEvent.Builder eventBuilder = new OutboundEvent.Builder();
OutboundEvent event = eventBuilder.name("event")
.comment("")
.build();
broadcaster.broadcast(event);
return eventOutput;
}
但是,FF 仍然在控制台上显示错误:加载页面时与 http://localhost:8080/broadcast 的连接中断。
您可以看到出现的错误 using this demo
大概a known bug
泽西岛 2.1.4,Java8,Tomcat8,Firefox 38.0.1
服务器:
@GET
@Produces(SseFeature.SERVER_SENT_EVENTS)
public EventOutput listenToBroadcast() {
final EventOutput eventOutput = new EventOutput();
this.broadcaster.add(eventOutput);
return eventOutput;
}
客户:
var source = new EventSource('broadcast');
source.addEventListener('event', function(event) {
alert('event');
}, false);
source.onopen = function() {
alert('connection open');
};
使用 Firefox 时,连接打开警报不会在页面加载时显示。 Firefox 在控制台中显示以下错误:Firefox 无法与位于 http://localhost:8080/broadcast 的服务器建立连接。 onopen 函数确实在第一个事件进入时被调用。在这种情况下,只有 onopen 函数被调用,而不是事件侦听器。
Chrome 工作正常。此外,此 demo 可与 Firefox 正常配合使用。
在页面加载时,在服务器发送事件之前,Firefox 中的“网络”选项卡显示它收到了 /broadcast SSE 端点的 OK 200,但不存在 headers。 Jersey 日志显示以下连接建立:
o.glassfish.jersey.filter.LoggingFilter : 11 * Server has received a request on thread http-nio-8080-exec-3
11 > GET http://localhost:8080/broadcast
11 > accept: text/event-stream
11 > accept-encoding: gzip, deflate
11 > accept-language: en-US,en;q=0.5
11 > cache-control: no-cache
11 > connection: keep-alive
11 > host: localhost:8080
11 > pragma: no-cache
11 > referer: http://localhost:8080/test_sse.html
11 > user-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0
11 * Server responded with a response on thread http-nio-8080-exec-3
11 < 200
11 < Content-Type: text/event-stream
我的客户端在创建 EventSource 连接后等待 EventSource.onOpen
事件 (new EventSource()
)。 Chrome 在连接打开后立即调用 onOpen
回调,但 Firefox 只会在服务器发送第一个事件时调用它。为了解决这个问题,我在服务器打开 SSE 连接后立即发送评论事件。 Firefox 获取此事件,这是无意义的,并调用 onOpen
函数。
这是我的服务器端客户端订阅代码:
@GET
@Produces(SseFeature.SERVER_SENT_EVENTS)
public EventOutput listenToBroadcast() {
final EventOutput eventOutput = new EventOutput();
this.broadcaster.add(eventOutput);
// firefox doesn't call the EventSource.onOpen callback when the connection is created, but it requires at least one event to be sent, so a
// meaningless comment event is used
OutboundEvent.Builder eventBuilder = new OutboundEvent.Builder();
OutboundEvent event = eventBuilder.name("event")
.comment("")
.build();
broadcaster.broadcast(event);
return eventOutput;
}
但是,FF 仍然在控制台上显示错误:加载页面时与 http://localhost:8080/broadcast 的连接中断。 您可以看到出现的错误 using this demo 大概a known bug