我想将 Spring Webflux 与 Server Sent Events 和 Netty 一起使用。我能够在客户端接收事件。但是当一个一个事件有很多,客户端取消订阅,那么服务端发送更多的事件,客户端没有收到。
年表:
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