检测 Flux 数据结束与错误

End*_*olf 5 eventsource spring-webflux angular5

目前正在研究使用 Angular 5 和 Spring 5 webflux 的 SSE。基本应用程序工作正常,但在调查错误处理时,我们注意到,EventSource在角度应用程序中,由于到达 Flux 数据流的末尾而导致弹簧关闭连接与发生错误之间没有任何区别(例如,在传输过程中终止应用程序)。

我们调查所依据的示例如下。

https://thepracticaldeveloper.com/2017/11/04/full-reactive-stack-ii-the-angularjs-client/

http://javasampleapproach.com/reactive-programming/angular-4-spring-webflux-spring-data-reactive-mongodb-example-full-reactive-angular-4-http-client-spring-boot-restapi-server# 25_服务

get中的onerror和completion函数都会在EventSourceSpring成功发送数据并到达流末尾时调用,然后关闭连接,或者当我们ctrl+c应用程序中流时,或者当我们随机抛出异常时发送数据的中间。

EventSource论证仅包含{type: 'error'}所有 3 种情况。

Bri*_*zel 2

据我了解,SSE流主要是无限流;该规范似乎没有提供向客户端发出流结束信号的标准方法(默认情况下它们将尝试重新连接)。

您可以在控制器中实现这一点,通过返回 aFlux<ServerSentEvent>并使用自定义事件终止通量:

return Flux.concat(
    fetchUserEvents(),
    Flux.just(ServerSentEvent.builder().event("disconnect").build()
);
Run Code Online (Sandbox Code Playgroud)

在客户端,您可以在完成后正确关闭连接,将所有其他用例保留为错误并让浏览器自动重新连接:

evtSource.addEventListener("disconnect", function(e) {
    evtSource.close();
}, false);
Run Code Online (Sandbox Code Playgroud)

这相当烦人,因此我提出了SPR-16761来改进 SSE 支持。