Jersey服务器发送的事件与Firefox不兼容

Jersey服务器发送的事件与Firefox不兼容,firefox,jersey,server-sent-events,Firefox,Jersey,Server Sent Events,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 eventOutp

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无法在上建立与服务器的连接。 当第一个事件出现时,会调用onopen函数。在这种情况下,只调用onopen函数,而不调用事件侦听器。 铬合金工作正常。此外,这也可以正确地与Firefox配合使用。 在页面加载时,在服务器发送事件之前,Firefox中的“网络”选项卡显示它收到了/broadcast SSE端点的OK 200,但不存在任何标题。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
o.glassfish.jersey.filter.LoggingFilter:11*服务器已在线程http-nio-8080-exec-3上收到请求
11>获取http://localhost:8080/broadcast
11>接受:文本/事件流
11>接受编码:gzip,deflate
11>接受语言:en-US,en;q=0.5
11>缓存控制:无缓存
11>连接:保持活动状态
11>主机:本地主机:8080
11>布拉格:没有缓存
11>参考:http://localhost:8080/test_sse.html
11>用户代理:Mozilla/5.0(Windows NT 6.1;WOW64;rv:38.0)Gecko/20100101 Firefox/38.0
11*服务器在线程http-nio-8080-exec-3上响应
11 < 200
11<内容类型:文本/事件流

在创建EventSource连接(
新建EventSource()
)后,我的客户端正在等待
EventSource.onOpen
事件。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仍然在控制台上显示错误:加载页面时,与的连接被中断。 您可以看到显示的错误 大概