简体   繁体   English

如何使用javascript-client关闭与sseflux的连接?

[英]How to close connection to sse flux with javascript-client?

I build a server-send-event endpoint with spring webflux. 我使用spring webflux构建了一个服务器发送事件端点。 My javascript app subscribes to that endpoint and receives the published events correctly. 我的JavaScript应用程序订阅了该端点并正确接收了已发布的事件。 BUT when call EventSource.close() it seems the publisher is not informed that the client closed the connection. 但是,当调用EventSource.close() ,似乎没有通知发布者客户端关闭了连接。

Hence, the publisher is still thinking that there is a subscription, and publishes events to that. 因此,发布者仍然认为存在订阅,并为此发布事件。

The event stream is potentially never ending, so I never get a complete signal on the publisher side, which means the client has to close the connection. 事件流可能永远不会结束,所以我永远不会在发布方收到完整的信号,这意味着客户端必须关闭连接。

I think that this happens because the close() call can't reach the underlying flux. 我认为发生这种情况是因为close()调用无法达到基础流量。

Do you have any solution or workaround for this problem? 您是否有解决此问题的解决方案?

@GetMapping(value = INSTANCE_UPDATE_ENDPOINT + "/{dealId}", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<ServerSentEvent<InstanceUpdatedQuery>> getInstanceUpdateEventStreamForDeal(@PathVariable final String dealId) {

       return this.instanceUpdatedApplicationEventFlux
               .filter(instanceUpdatedEvent -> instanceUpdatedEvent.getDealId().equals(dealId))
               .map(instanceUpdatedEvent -> ServerSentEvent.<InstanceUpdatedQuery>builder()
                       .id(instanceUpdatedEvent.getId())
                       .retry(Duration.ofSeconds(1))
                       .data(new InstanceUpdatedQuery(instanceUpdatedEvent))
                       .build())
}

您应该添加一个处理程序以捕获doOnNext(Signal),然后检测关闭信号并对其进行响应

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

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