Javascript Java服务器未使用OutboundSseEvent/JAX-RS服务器发送事件(SSE)服务发送消息事件
我已经在Java服务器中设置了SSE(服务器发送事件)。客户端构建在VueJS上,并使用EventSource包从服务器接收事件。我正在客户端上使用EventSourcePolyfill npm包。这个包允许我们在请求中发送授权头(针对我们的安全方案) 客户端向我们的SSE服务发送一个请求,并接收一个“打开”类型的事件。然后,SSE不再发送其他事件,或者确实发送事件,而浏览器未检测到这些事件。为什么会这样?修复的建议? 没有错误消息 这种行为发生在Chrome、Firefox和Edge中,因此可能与浏览器无关Javascript Java服务器未使用OutboundSseEvent/JAX-RS服务器发送事件(SSE)服务发送消息事件,javascript,java,server-sent-events,eventsource,Javascript,Java,Server Sent Events,Eventsource,我已经在Java服务器中设置了SSE(服务器发送事件)。客户端构建在VueJS上,并使用EventSource包从服务器接收事件。我正在客户端上使用EventSourcePolyfill npm包。这个包允许我们在请求中发送授权头(针对我们的安全方案) 客户端向我们的SSE服务发送一个请求,并接收一个“打开”类型的事件。然后,SSE不再发送其他事件,或者确实发送事件,而浏览器未检测到这些事件。为什么会这样?修复的建议? 没有错误消息 这种行为发生在Chrome、Firefox和Edge中,因此可
Our client method...
testSSE() {
console.log("in testSSE()");
console.log("apikey: " + store.user.apikey);
var EventSource = EventSourcePolyfill;
const evtSource = new EventSource(
"http://localhost:8081/api/ssetest/test",
{ headers: { Authorization: "Bearer " + store.user.apikey } }
);
evtSource.onmessage = function(event) {
console.log("received message");
console.log("event.data: " + event.data);
};
//This is the only function that gets hit
evtSource.addEventListener("open", function(event) {
console.log("open event");
console.log(Object.keys(event));
console.log(event.type);
});
evtSource.addEventListener("message", function(event) {
console.log("message event");
});
evtSource.addEventListener("stringEvent", function(event) {
console.log("received ping event");
console.log("event.data: " + event.data);
});
evtSource.onerror = function(err) {
console.error("EventSource failed:", JSON.stringify(err));
};
}
这是我们的服务器代码。。。
@路径(“ssetest”)
公共类SSETestService扩展了SecureService{
@GET
@Path("/test")
@Produces("text/event-stream")
public void runTest(@Context Sse sse, @Context SseEventSink sseEventSink)
{
int lastEventId = 0;// ..;
while (lastEventId < 10) {
OutboundSseEvent stringEvent = sse.newEventBuilder()
.name("" + lastEventId)
.mediaType(MediaType.APPLICATION_JSON_TYPE)
.data("hello world")
.id(String.valueOf(lastEventId))
.reconnectDelay(4000) // .comment("price change")
.build();
sseEventSink.send(stringEvent);
lastEventId++;
TimeUnit.SECONDS.sleep(1);
}
}
我也遇到了类似的问题。将@products value转换为“application/stream+json”为我解决了这个问题
Browser console output...
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/iR57U.png