websocket closing connection automatically [closed]
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.