我正在尝试设置websocket客户端/服务器。 问题出现的原因是会话中不活动,在大约30~60秒之后没有数据传输,客户端关闭连接,但例外情况如下:
org.eclipse.jetty.websocket.api.WebSocketTimeoutException: Timeout on Read
at org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection.onReadTimeout(AbstractWebSocketConnection.java:505)
at org.eclipse.jetty.io.AbstractConnection.onFillInterestedFailed(AbstractConnection.java:258)
at org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection.onFillInterestedFailed(AbstractWebSocketConnection.java:481)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.failed(AbstractConnection.java:415)
at org.eclipse.jetty.io.FillInterest.onFail(FillInterest.java:100)
at org.eclipse.jetty.io.AbstractEndPoint.onIdleExpired(AbstractEndPoint.java:146)
at org.eclipse.jetty.io.IdleTimeout.checkIdleTimeout(IdleTimeout.java:153)
at org.eclipse.jetty.io.IdleTimeout$1.run(IdleTimeout.java:50)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:178)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:292)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
ES:套接字关闭:[1001]空闲超时
没有任何意义,因为我在客户端和服务器端尝试将maxIdleTimeout设置为更大的值,即使在会话建立之后,我也会检查它:
client.setMaxIdleTimeout(0);
我尝试过不同的值,而不是上面的“0”,但无济于事。
答案 0 :(得分:2)
此问题已有2年但仍然存在。其背后的原因是Jetty默认websocket超时为5分钟(参见org.eclipse.jetty.websocket.api.WebSocketPolicy),而在Tomcat上则为0。
要解决此问题,您可以在WebSocket客户端代码中执行以下操作:
let someLine = CAShapeLayer()
func setupLine(inout line:CAShapeLayer, startingPath: CGPath) {
line.path = startingPath
line.strokeColor = UIColor.whiteColor().CGColor
line.fillColor = nil
line.lineWidth = 1
}
setupLine(&someLine, startingPath: somePath)
但是,实施 WebSocketContainer container = ContainerProvider.getWebSocketContainer();
Session session = container.connectToServer(this, endpointURI);
session.setMaxIdleTimeout(0);
并执行@OnError
并再次连接以保持连接稳定可能是个好主意。此外,Jetty的session.close()
派生自RuntimeException,您可以检查Throwable的实例并仅在RuntimeExceptions上重新连接。对于服务器等情况下的“真实”错误处理应该在WebSocketTimeoutException