Spring websocket EOFException

时间:2017-10-23 11:56:00

标签: spring-boot tomcat8 spring-websocket

我的项目是使用spring-boot网络套接字和嵌入式tomcat来实现聊天服务器。一切都很好,但有时我得到EOFException,然后客户端无法向聊天服务器发送消息,直到我重新启动tomcat然后一切正常。我不知道何时会发生EOFException。请帮助我

  

[TRACE] 2017-10-23 06:17:10.707 [http-nio-7755-exec-4]   NativeWebSocketSession - 发送TextMessage payload = [{“result”:..],   byteCount = 164,last = true],StandardWebSocketSession [id = 42b,uri = / chat]   [DEBUG] 2017-10-23 06:17:29.670 [http-nio-7755-exec-8]   LoggingWebSocketHandlerDecorator - 中的传输错误   StandardWebSocketSession [id = 42b,uri = / chat] java.io.EOFException:null     在   org.apache.tomcat.util.net.NioEndpoint $ NioSocketWrapper.fillReadBuffer(NioEndpoint.java:1242)   〜[tomcat-embed-core-8.5.16.jar!/:8.5.16] at   org.apache.tomcat.util.net.NioEndpoint $ NioSocketWrapper.read(NioEndpoint.java:1182)   〜[tomcat-embed-core-8.5.16.jar!/:8.5.16] at   org.apache.tomcat.websocket.server.WsFrameServer.onDataAvailable(WsFrameServer.java:72)   〜[tomcat-embed-websocket-8.5.16.jar!/:8.5.16] at   org.apache.tomcat.websocket.server.WsFrameServer.doOnDataAvailable(WsFrameServer.java:171)   〜[tomcat-embed-websocket-8.5.16.jar!/:8.5.16] at   org.apache.tomcat.websocket.server.WsFrameServer.notifyDataAvailable(WsFrameServer.java:151)   〜[tomcat-embed-websocket-8.5.16.jar!/:8.5.16] at   org.apache.tomcat.websocket.server.WsHttpUpgradeHandler.upgradeDispatch(WsHttpUpgradeHandler.java:148)   [tomcat-embed-websocket-8.5.16.jar!/:8.5.16] at   org.apache.coyote.http11.upgrade.UpgradeProcessorInternal.dispatch(UpgradeProcessorInternal.java:54)   [tomcat-embed-core-8.5.16.jar!/:8.5.16] at   org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:53)   [tomcat-embed-core-8.5.16.jar!/:8.5.16] at   org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:868)   [tomcat-embed-core-8.5.16.jar!/:8.5.16] at   org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1455)   [tomcat-embed-core-8.5.16.jar!/:8.5.16] at   org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)   [tomcat-embed-core-8.5.16.jar!/:8.5.16] at   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)   [?:1.8.0_131] at   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:624)   [?:1.8.0_131] at   org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)   [tomcat-embed-core-8.5.16.jar!/:8.5.16] at   java.lang.Thread.run(Thread.java:748)[?:1.8.0_131] [DEBUG] 2017-10-23   06:17:29.671 [http-nio-7755-exec-8] LoggingWebSocketHandlerDecorator -   StandardWebSocketSession [id = 42b,uri = / chat]关闭   CloseStatus [code = 1006,reason = null]

2 个答案:

答案 0 :(得分:2)

是的,我解决了。在客户端或服务器中断或停止而不调用关闭套接字方法(可能丢失互联网,或使用套接字时关闭笔记本电脑或移动设备)之后,将发生此异常。所以如果我们想解决这个问题,我们必须实现ping / pong机制,如果我们无法从客户端获得pong响应,我们将关闭此套接字。另一种方法,我们可以捕获这个异常,然后我们将关闭旧套接字。

谢谢, 安迪

答案 1 :(得分:0)

好吧,我无法遵循。所以这是我的解决方案。

当用户会话结束时,我得到了神秘的EOFExceptions,我的解决方案是加强websocket onError方法中的错误处理(使用更详细的onError函数)。

我相信我对Javacodegeeks的扎根和发情最终将我指向了正确的方向。

@OnError
public void onError(Session sess, Throwable e) {
    Throwable cause = e.getCause();
    /* normal handling... */
    if (cause != null)
        System.out.println("Error-info: cause->" + cause);
    try {
        // Likely EOF (i.e. user killed session) 
        // so just Close the input stream as instructed
        sess.close();
    } catch (IOException ex) {
        System.out.println("Handling eof, A cascading IOException was caught: " + ex.getMessage());
        ex.printStackTrace();
    } finally {
        System.out.println("Session error handled. (likely unexpected EOF) resulting in closing User Session.");

    }
}