简体   繁体   English

Java 服务器未使用 OutboundSseEvent / JAX-RS 服务器发送事件 (SSE) 服务发送消息事件

[英]Java server not sending message events with OutboundSseEvent / JAX-RS server-sent event (SSE) service

I have set up SSE (server-sent-events) in a Java server.我在 Java 服务器中设置了 SSE(服务器发送事件)。 The client is built on VueJS and uses the EventSource package to receive events from the server.客户端基于 VueJS 构建,并使用 EventSource package 从服务器接收事件。 I am using the EventSourcePolyfill npm package https://www.npmjs.com/package/event-source-polyfill on the client.我在客户端上使用 EventSourcePolyfill npm package https://www.npmjs.com/package/event-source-polyfill This package allows us to send an authorization header (for our security scheme) with the request.这个 package 允许我们在请求中发送授权 header(用于我们的安全方案)。

The client sends a request to our SSE service and receives a single event of type "open".客户端向我们的 SSE 服务发送一个请求并接收一个“open”类型的事件。 The SSE then sends no further events or does send events and the browser does not detect them.然后 SSE 不再发送事件或确实发送事件并且浏览器没有检测到它们。 Why is this?为什么是这样? Suggestions for fixing?修复建议? There are no error messages.没有错误消息。

This behavior occurs in Chrome, Firefox and Edge, so it probably has nothing to do with the browser.这种行为出现在 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));
      };
    }

This is our server code... @Path("ssetest") public class SSETestService extends SecureService {这是我们的服务器代码... @Path("ssetest") public class SSETestService extends 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);

    }
}

} }

Browser console output...
[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/iR57U.png

I had a similar issue.我有一个类似的问题。 Switching the @Produces value to "application/stream+json" solved it for me.将@Produces 值切换为“application/stream+json”为我解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM