聊天系统使用WebSocket进行消息传递,应用服务器使用两台做负载均衡,一台Web服务器部署Nginx做代理,登录系统之后总是会报java.io.EOFException异常,异常内容如下,从错误可以看出是WebSocket走了OnError方法,断开了连接,因为是使用的Nginx做代理,怀疑是Nginx将连接断开了,nginx默认60秒断开连接,尝试设置nginx的断连时间更长一点比如1小时。

  1. java.io.EOFException
  2. at org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.fillReadBuffer(NioEndpoint.java:1262)
  3. at org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.read(NioEndpoint.java:1196)
  4. at org.apache.tomcat.websocket.server.WsFrameServer.onDataAvailable(WsFrameServer.java:72)
  5. at org.apache.tomcat.websocket.server.WsFrameServer.doOnDataAvailable(WsFrameServer.java:171)
  6. at org.apache.tomcat.websocket.server.WsFrameServer.notifyDataAvailable(WsFrameServer.java:151)
  7. at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler.upgradeDispatch(WsHttpUpgradeHandler.java:148)
  8. at org.apache.coyote.http11.upgrade.UpgradeProcessorInternal.dispatch(UpgradeProcessorInternal.java:54)
  9. at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:53)
  10. at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:800)
  11. at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1471)
  12. at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
  13. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
  14. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
  15. at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
  16. at java.lang.Thread.run(Thread.java:745)
  1. location / {
  2. root html;
  3. index index.html index.htm;
  4. proxy_pass http://wslb;
  5. proxy_http_version 1.1;
  6. proxy_read_timeout 3600s;
  7. proxy_set_header Upgrade $http_upgrade;
  8. proxy_set_header Connection "upgrade";
  9. }

重启nginx,观察登录后的WebSocket连接情况,似乎没有再自动断连。

发表评论

邮箱地址不会被公开。 必填项已用*标注