WebSocket发生EOFException异常
聊天系统使用WebSocket进行消息传递,应用服务器使用两台做负载均衡,一台Web服务器部署Nginx做代理,登录系统之后总是会报java.io.EOFException异常,异常内容如下,从错误可以看出是WebSocket走了OnError方法,断开了连接,因为是使用的Nginx做代理,怀疑是Nginx将连接断开了,nginx默认60秒断开连接,尝试设置nginx的断连时间更长一点比如1小时。
-
java.io.EOFException
-
at org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.fillReadBuffer(NioEndpoint.java:1262)
-
at org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.read(NioEndpoint.java:1196)
-
at org.apache.tomcat.websocket.server.WsFrameServer.onDataAvailable(WsFrameServer.java:72)
-
at org.apache.tomcat.websocket.server.WsFrameServer.doOnDataAvailable(WsFrameServer.java:171)
-
at org.apache.tomcat.websocket.server.WsFrameServer.notifyDataAvailable(WsFrameServer.java:151)
-
at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler.upgradeDispatch(WsHttpUpgradeHandler.java:148)
-
at org.apache.coyote.http11.upgrade.UpgradeProcessorInternal.dispatch(UpgradeProcessorInternal.java:54)
-
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:53)
-
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:800)
-
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1471)
-
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
-
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
-
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
-
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
-
at java.lang.Thread.run(Thread.java:745)
-
location / {
-
root html;
-
index index.html index.htm;
-
proxy_pass http://wslb;
-
proxy_http_version 1.1;
-
proxy_read_timeout 3600s;
-
proxy_set_header Upgrade $http_upgrade;
-
proxy_set_header Connection "upgrade";
-
-
}
重启nginx,观察登录后的WebSocket连接情况,似乎没有再自动断连。