Jersey 2.1.4,Java 8,Tomcat 8,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中的“网络”选项卡显示它已收到/ 200广播SSE端点的OK,但没有标题。 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
答案 0 :(得分:1)
我的客户端在创建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