繁体   English   中英

已发送泽西岛2服务器+客户端事件源无法正常工作

[英]Jersey 2 Server Sent + client side Event source not working properly

我正在使用jersey 2来实现发送的服务器。 当我卷曲时,我会得到预期的延迟一秒钟的正确响应。 但是,当我尝试从客户端使用事件源时,它等待所有事件完成,然后调用onmessage。 它还反复进行服务器调用..以下是我正在使用的服务器端和客户端代码。 服务器端球衣代码

    @GET
@Path("/serverSentCheck")
@Produces(SseFeature.SERVER_SENT_EVENTS)
@ManagedAsync
public EventOutput serverSentCheck() {
    final EventOutput eventOutput = new EventOutput();
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                for (int i = 0; i < 5; i++) {
                    Thread.sleep(1000);
                    final OutboundEvent.Builder eventBuilder
                    = new OutboundEvent.Builder();
                   // eventBuilder.name("message-to-client");
                    eventBuilder.data(String.class,
                        "Hello world " + i + "!");
                    final OutboundEvent event = eventBuilder.build();
                    eventOutput.write(event);
                }
            } catch (IOException | InterruptedException e) {
                throw new RuntimeException(
                    "Error when writing the event.", e);
            } finally {
                try {
                    eventOutput.close();
                } catch (IOException ioClose) {
                    throw new RuntimeException(
                        "Error when closing the event output.", ioClose);
                }
            }
        }
    }).start();
    return eventOutput;
}

客户端事件源代码

var source = new EventSource(" https://localhost:7080/api/v1/service/serverSentCheck");
source.onmessage = function(event) {
    console.log(event.data);
    document.getElementById("result").innerHTML += event.data + "<br>";
};

卷曲命令多数民众赞成在正常工作

curl  --insecure -H "Accept:text/event-stream" https://localhost:7080/api/v1/service/serverSentCheck

挠了一下头后,我得以找出问题所在。 此处提及是为了对遇到相同问题的其他人有所帮助。 在我的服务器端,默认情况下启用了GZIP。 当我更改为每个API的压缩,并且排除了被压缩的服务器后,一切正常。

暂无
暂无

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

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