检测 Flux 数据的结束 vs.错误 [英] Detecting end of Flux data v.s. error

查看:25
本文介绍了检测 Flux 数据的结束 vs.错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Currently looking at SSE using Angular 5 and Spring 5 webflux. The basic application is working correctly, but whilst investigating error handling we've noticed that the EventSource in the angular application doesn't see any difference between spring closing the connection due to reaching the end of the Flux stream of data, and an error occuring (e.g. terminating the application mid transfer).

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

The examples we've based our investigations on are the following.

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_Service

EventSource 中的 onerror 和完成函数都会在 spring 成功发送数据并到达流的末尾时调用,然后流的末尾关闭连接,或者当我们 ctrl+c 应用中途,或者当我们在发送数据中途随机抛出异常时.

Both onerror and the completion function in the EventSource get called either when the data is send by spring successfully and reaches the end of the stream, which then closes the connection, or when we ctrl+c the application mid stream, or when we throw an exception randomly in the middle of sending data.

EventSource 参数在所有 3 种情况下只包含 {type: 'error'}.

The EventSource argument just contains {type: 'error'} in all 3 cases.

推荐答案

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

From what I understand, SSE streams are mainly about infinite streams; the spec doesn't seem to offer a standard way of signaling the end of a stream to clients (they will try to reconnect by default).

您可以在控制器中实现这一点,方法是返回一个 Flux 并使用自定义事件终止通量:

You could implement that in your controller, by returning a Flux<ServerSentEvent> and terminating the flux with a custom event:

return Flux.concat(
    fetchUserEvents(),
    Flux.just(ServerSentEvent.builder().event("disconnect").build()
);

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

On the client side, you could correctly close the connection when you're done, leaving all other use cases as errors and letting the browser reconnecting automatically:

evtSource.addEventListener("disconnect", function(e) {
    evtSource.close();
}, false);

这很烦人,所以我提出了 SPR-16761 来改进上证所支持.

This is rather annoying, so I've raised SPR-16761 to improve SSE support there.

这篇关于检测 Flux 数据的结束 vs.错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆