Spring Webflux检测到SSE连接取消,并有延迟

问题描述 投票:0回答:1

我想使用Spring Webflux与服务器发送事件和Netty。我能够在客户端接收事件。但是当有很多事件一个接一个,客户端取消订阅时,那么服务器会发送更多的事件,而客户端却收不到这些事件。

Сhronology。

0 - Server checks if the downstream cancelled. It is not cancelled and the server sends event A 
1 - Client receives event A
3 - Client closes SSE connection 
4 - Server checks if the downstream cancelled. It is actually cancelled, but server does not detect it and sends event B 
5 - Server checks if the downstream cancelled. It is actually cancelled, and server detects it and does not send event C

这里的问题是,我无法确定事件B是否被发送,因为在消息发送的那一刻,没有证据表明连接已经关闭.所以,问题是我如何避免这种情况?我怎样才能知道在消息发送的那一刻,消息是否被送达或者连接是否真的打开了?

这是我的控制器。

 @GetMapping
  public Flux<ServerSentEvent> stream()  { 
    return Flux.<ServerSentEvent>push(emitter -> {
      long counter = 0;
      while (!emitter.isCancelled()) {
        emitter.next(ServerSentEvent.builder(++counter).build());
        System.out.println(counter);
       }
    }, OverflowStrategy.ERROR);
  }

这是客户端日志的尾巴。

...

data:212

data:213

data:214

这是服务器日志的尾巴。

...
213
214
215
216
netty spring-webflux server-sent-events spring-reactor
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.