I'm trying to setup a websocket client/server. The problem arises due to inactivity in the session where after around 30~60s of no transfer of data, the client closes the connection, with the exception:
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: Socket Closed: [1001] Idle Timeout
It makes no sense since I've tried both on the client and the server side to set the maxIdleTimeout to much larger values, and even after the session is established, I check it:
client.setMaxIdleTimeout(0);
I've tried different values instead of the "0" above to no avail.
This issue is 2 years old but still exists. The reason behind it is Jetty default websocket timeout is 5 minutes (see org.eclipse.jetty.websocket.api.WebSocketPolicy), while on Tomcat it is 0.
To fix it, in your WebSocket client code you can do something along these lines:
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
Session session = container.connectToServer(this, endpointURI);
session.setMaxIdleTimeout(0);
However, it might be a good idea to implement @OnError
and do session.close()
and connect again to keep the connection stable. Also, Jetty's WebSocketTimeoutException
is derived from RuntimeException, you might check instance of Throwable and reconnect on RuntimeExceptions only. And "real" error processing for situations like server is down etc. should be in IOException processing of container.connectToServer()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With