websocket closing connection automatically
Solution 1
In answer to your third question: your client wants to be able to cope with temporary network problems anyway, e.g. let's say the user closes their laptop between meetings which hibernates it, or the network simply goes down temporarily.
The solution is to listen to onclose
events on the web socket client and when they occur, set a client side timeout to re-open the connection, say in a second:
function setupWebSocket(){
this.ws = new WebSocket('wss://host:port/path');
this.ws.onerror = ...;
this.ws.onopen = ...;
this.ws.onmessage = ...;
this.ws.onclose = function(){
setTimeout(setupWebSocket, 1000);
};
}
Solution 2
You need to send ping messages from time to time. I think the default timeout is 300 seconds. Sending websocket ping/pong frame from browser
Solution 3
I found another, rather quick and dirty, solution.
If you use the low level approach to implement the WebSocket and you Implement the onOpen
method yourself you receive an object implementing the WebSocket.Connection
interface. This object has a setMaxIdleTime method which you can adjust.
Solution 4
You can actually set the timeout interval at the Jetty server side configuration using the WebSocketServletFactory
instance. For example:
WebSocketHandler wsHandler = new WebSocketHandler() {
@Override
public void configure(WebSocketServletFactory factory) {
factory.getPolicy().setIdleTimeout(1500);
factory.register(MyWebSocketAdapter.class);
...
}
}
Solution 5
Just found the solution to this for myself. What you want to set is the maxIdleTime of WebSocketServlet, in millis. How to do that depends on how you config your servlet. With Guice ServletModule you can do something like this for timeout of 10 hours:
serve("ws").with(MyWSServlet.class,
new HashMap<String, Sring>(){{ put("maxIdleTime", TimeUnit.HOURS.toMillis(10) + ""); }});
Anything <0 is infinite idle time I believe.
Related videos on Youtube
Doua Beri
Updated on July 05, 2022Comments
-
Doua Beri almost 2 years
I'm building an application in java that has an embedded websocket server based on jetty. The client is the default websocket implementation in google chrome. Everything is working ok, only if there is no transfer between server and client after a certain time the connection is closed. I'm not sure who's closing the connection: the jetty server or the chrome browser.
The solution to this I think is to send a message every x seconds, but I'm opened to better solutions.
SO... my questions are:
Is this something that the websocket protocol requires and in this case the chrome browser is closing my connection?
Is this something that is more jetty related and has more or less to do with the websocket protocol? In this case how do I disable this in jetty?
Is there another problem??
Thanks
UPDATE: even if I send 1 message/second still the connection is closed
-
ndeverge over 12 yearsDo you have any proxy between the client and the server ? Proxies are known to sometimes close websockets (stackoverflow.com/questions/9017113/…)
-
Ant Kutschera about 9 yearsI'm using Jetty and I have the same problem. No proxy - I have a server on localhost with the browser on the same machine.
-
Plastic over 6 yearsmmm are you testing it on Internet Explorer?? cause I'm still suffering about this strange IE beahvior: connect.microsoft.com/IE/feedback/details/804653/…
-
Doua Beri over 12 yearshey thanks.. but this doesn't help me. I've just tried with 1 message every second and the connection is still closing
-
kanaka over 12 yearsDavid, can you back that up? My understanding is that TCP leaves policy decisions like idle timeout up to the application, OS, and network infrastructure (TCP tries to be policy agnostic). Perhaps you are thinking of keep-alive which is periodic messages to ensure that the connection hasn't died silently (which eventually timeout and close the connection usually after 2 hours).
-
David Grayson over 12 years@kanaka I read that somewhere but I can't really back it up. I know for sure that when I was writing my own websocket server with Python, I found that I needed to send an empty message once in a while to prevent it from disconnecting.
-
David Hoelzer almost 5 yearsThis is not part of TCP/IP, but can be the result of a stateful firewall losing the state of the connection because it has been idle for too long. Likely, you are thinking of TCP Keepalives, which solve this problem.