I have the following code (WebSocket server-side endpoint):
@OnOpen public void open(Session session) { session.setMaxIdleTimeout(-1L); ... }
At the client side, I have only:
var websocket = new WebSocket("wss://dummy.org"); websocket.onmessage = onMessage;
And yet, the session
gets closed after 1 or 2 minutes of inactivity. Am I missing something here? Should I tweak something at the client side?
Edit
I also tried at the client side this:
websocket.onclose = reconnect; function reconnect() { websocket = new WebSocket("wss://dummy.org"); websocket.onclose = function() { reconnect(); }; }
... and (of course) it does not solve the issue.
Edit 2
I also tried to send from the client side a ping message, to which the endpoint responds with a pong message:
(function(){ function sendPing() { websocket.send("{\"action\":\"ping\"}"); setTimeout(sendPing, 10 * 1000); // 10 seconds. } setTimeout(sendPing, 10 * 1000); // 10 seconds. })();
1 Answers
Answers 1
There are several things that could end to a closed session.
One reason could be the web-application or container configuration. For example if you use tomcat, check the timeout setting in server.xml
file. Or another example is when nGinx is in charge of routing the connection. There are also some configurations on stand-alone websocket servers, depending on your implementation, you might want to check them.
Second reason could be client-side configuration. Do a little search on how to set timeout in your javascript code. There are already many articles and answers about it on internet, I cant provide you one myself. -- you could also test with a java client and see if the problem is from server code or client one. (of course, that also needs some configuration)
Your internet connection (unless you are testing locally) could be next reason for connection failures. Although usually you can recognize this by overriding/adding onError(Exception e)
.
There is one more thing that you must implement in order to keep websocket connections alive: a heartbeat
system. Most standard websocket libraries support ping
and pong
. On each side (usually server side) you can send ping frames to the other side and they automatically send back a pong frame. This helps keeping connection alive. This method also can help server side realize if the client is actually out there!!!
And as you already said, do not solve your problems by ignoring them. Making an alive and healthy websocket connection takes some effort and a time to deal with many things which by ignoring them you will end up with bigger troubles.
I hope this helps.
0 comments:
Post a Comment