Dan*_*zan 2 firefox jersey server-sent-events
泽西岛 2.1.4、Java 8、Tomcat 8、火狐 38.0.1
服务器:
@GET
@Produces(SseFeature.SERVER_SENT_EVENTS)
public EventOutput listenToBroadcast() {
final EventOutput eventOutput = new EventOutput();
this.broadcaster.add(eventOutput);
return eventOutput;
}
Run Code Online (Sandbox Code Playgroud)
客户:
var source = new EventSource('broadcast');
source.addEventListener('event', function(event) {
alert('event');
}, false);
source.onopen = function() {
alert('connection open');
};
Run Code Online (Sandbox Code Playgroud)
使用 Firefox,页面加载时不会显示连接打开警报。Firefox 在控制台中显示以下错误: Firefox 无法建立与服务器的连接http://localhost:8080/broadcast。当第一个事件到来时,onopen 函数会被调用。在这种情况下,仅调用 onopen 函数,而不调用事件侦听器。
Chrome 工作正常。此外,该演示可以在 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
Run Code Online (Sandbox Code Playgroud)
EventSource.onOpen我的客户端在创建 EventSource 连接 ( ) 后正在等待事件new EventSource()。onOpenChrome在连接打开后立即调用回调,但 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;
}
Run Code Online (Sandbox Code Playgroud)
但是,FF在控制台上仍然显示错误:加载页面时,与http://localhost:8080/broadcast的连接被中断。您可以看到使用此演示显示的错误 可能是一个已知的错误
| 归档时间: |
|
| 查看次数: |
3246 次 |
| 最近记录: |