服务器发送事件 - 在几次断开连接后流未重新连接

Posted

技术标签:

【中文标题】服务器发送事件 - 在几次断开连接后流未重新连接【英文标题】:Server sent event - Stream is not reconnecting after few disconnections 【发布时间】:2019-11-03 07:25:40 【问题描述】:

我在开发前端应用程序时使用此 https://github.com/renatoaguimaraes/spring-reactive-kafka-sse.git 构建了一个 Spring Boot 应用程序我在几次成功重新连接后不断断开并重新连接 SSE 流我遇到错误并且流没有自动连接

java.io.IOException: An established connection was aborted by the 
software in your host machine
at sun.nio.ch.SocketDispatcher.write0(Native Method)
at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:51)
at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:93)
at sun.nio.ch.IOUtil.write(IOUtil.java:65)
at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:471)
at org.apache.tomcat.util.net.NioChannel.write(NioChannel.java:134)
at org.apache.tomcat.util.net.NioBlockingSelector.write(NioBlockingSelector.java:101)
at org.apache.tomcat.util.net.NioselectorPool.write(NioSelectorPool.java:157)
at org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.doWrite(NioEndpoint.java:1276)
at org.apache.tomcat.util.net.SocketWrapperBase.doWrite(SocketWrapperBase.java:670)
at org.apache.tomcat.util.net.SocketWrapperBase.flushBlocking(SocketWrapperBase.java:607)
at org.apache.tomcat.util.net.SocketWrapperBase.flush(SocketWrapperBase.java:597)
at org.apache.coyote.http11.Http11OutputBuffer$SocketOutputBuffer.flush(Http11OutputBuffer.java:646)
at org.apache.coyote.http11.filters.ChunkedOutputFilter.flush(ChunkedOutputFilter.java:169)
at org.apache.coyote.http11.Http11OutputBuffer.flush(Http11OutputBuffer.java:252)
at org.apache.coyote.http11.Http11Processor.flush(Http11Processor.java:1564)
at org.apache.coyote.AbstractProcessor.action(AbstractProcessor.java:352)
at org.apache.coyote.Response.action(Response.java:173)
at org.apache.catalina.connector.OutputBuffer.doFlush(OutputBuffer.java:317)
at org.apache.catalina.connector.OutputBuffer.flush(OutputBuffer.java:284)
at org.apache.catalina.connector.CoyoteOutputStream.flush(CoyoteOutputStream.java:118)
at sun.nio.cs.StreamEncoder.implFlush(StreamEncoder.java:297)
at sun.nio.cs.StreamEncoder.flush(StreamEncoder.java:141)
at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:229)
at org.springframework.util.StreamUtils.copy(StreamUtils.java:121)
at org.springframework.http.converter.StringHttpMessageConverter.writeInternal(StringHttpMessageConverter.java:103)
at org.springframework.http.converter.StringHttpMessageConverter.writeInternal(StringHttpMessageConverter.java:43)
at org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:224)
at org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitterReturnValueHandler$HttpMessageConvertingHandler.sendInternal(ResponseBodyEmitterReturnValueHandler.java:199)
at org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitterReturnValueHandler$HttpMessageConvertingHandler.send(ResponseBodyEmitterReturnValueHandler.java:189)
at org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter.sendInternal(ResponseBodyEmitter.java:189)
at org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter.send(ResponseBodyEmitter.java:183)
at org.springframework.web.servlet.mvc.method.annotation.SseEmitter.send(SseEmitter.java:133)
at org.springframework.web.servlet.mvc.method.annotation.ReactiveTypeHandler$SseEmitterSubscriber.send(ReactiveTypeHandler.java:341)
at org.springframework.web.servlet.mvc.method.annotation.ReactiveTypeHandler$AbstractEmitterSubscriber.run(ReactiveTypeHandler.java:283)
at java.lang.Thread.run(Thread.java:748)

【问题讨论】:

【参考方案1】:

在创建 EmitterProcesser 时设置为 autoCancel=false

private  EmitterProcessor<ServerSentEvent<String>> emitter = EmitterProcessor.create(false);

【讨论】:

以上是关于服务器发送事件 - 在几次断开连接后流未重新连接的主要内容,如果未能解决你的问题,请参考以下文章

WebSocket 在几个时间间隔之间不断断开连接

Ktor sse 客户端断开连接

如何从客户端断开和重新连接 socket.io?

通过 Nginx 在 Node.js 上的服务器发送事件连接超时

使用openfire在xmpp中几秒钟后连接断开

宽带连接错误629