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

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

问题描述

当前使用Angular 5和Spring 5 webflux查看SSE.基本应用程序正常运行,但是在研究错误处理时,我们注意到在角度应用程序中,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/

当spring成功发送数据并到达流的末尾,然后关闭连接时,或者当我们 ctrl 时,都将调用onerror和EventSource中的完成函数. + 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.

在所有3种情况下,EventSource参数仅包含{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<ServerSentEvent>并使用自定义事件终止通量:

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 来进行改进那里有SSE支持.

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

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

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