Auto close connection

I am creating an application in java that has a built-in websocket server based on the pier. The client is the default implementation of websocket in google chrome. Everything works fine, only if there is no transmission between the server and the client after a certain time, the connection is closed. I'm not sure who closes the connection: the quay server or the Chrome browser.

The solution to this, I think, is to send a message every x seconds, but I am open to better solutions.

SO ... my questions are:

  • Is this what the websocket protocol requires, in which case does the Chrome browser close my connection?

  • Is it something more connected with the pier and more or less connected with the websocket protocol? In this case, how to disable it in the berth?

  • Is there any other problem?

thank

UPDATE: even if I send 1 message per second, the connection closes

+66
java javascript google-chrome jetty websocket
Jan 29 '12 at 19:44
source share
9 answers

In response to the third question: your client wants in any case to cope with temporary network problems, for example. let's say a user closes his laptop between meetings that are sleeping with him, or the network just temporarily shuts down.

The solution is to listen for onclose events on the web socket client, and when they occur, set a timeout on the client side to reopen the connection, say per 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); }; } 
+38
Mar 08 '15 at 21:45
source share

You need to send ping messages from time to time. I think the default timeout is 300 seconds. Send ping / pong webcam from browser

+15
Oct 26
source share

I found another, fairly quick and dirty solution. If you use a low-level approach to implement WebSocket and implement the onOpen method onOpen , you get an object that implements the WebSocket.Connection interface. This object has a setMaxIdleTime method that you can customize.

+9
Aug 15 '12 at 8:40
source share

In fact, you can set a timeout interval on the Jetty server side using an instance of WebSocketServletFactory. For example:

  WebSocketHandler wsHandler = new WebSocketHandler() { @Override public void configure(WebSocketServletFactory factory) { factory.getPolicy().setIdleTimeout(1500); factory.register(MyWebSocketAdapter.class); ... } } 
+7
Apr 04 '14 at 19:12
source share

Just found a solution for myself. What you want to set is maxIdleTime for WebSocketServlet, in milliseconds. How to do this depends on how you configure your servlet. With Guice ServletModule, you can do something similar for a 10 hour timeout:

 serve("ws").with(MyWSServlet.class, new HashMap<String, Sring>(){{ put("maxIdleTime", TimeUnit.HOURS.toMillis(10) + ""); }}); 

Anything and lt 0 is infinite downtime, I suppose.

+4
Jun 27 '12 at 17:57
source share

I believe this is a Jetty problem. I did not see any browsers close WebSocket connections due to inactivity, and I did not see other WebSocket servers that did not meet WebSocket connections.

Jetty (mainly) focuses on creating servlets for HTTP-based applications. In this context, HTTP connections should be cleaned up quite aggressively, and HTTP is not intended for long-term connections, so having a short timeout by default is reasonable.

I did not see the exact problem that you described (closing even with activity), but I see that WebSocket connections are closed after 30 seconds of inactivity. It is possible that in older versions of Jetty or in the current version, for some other reason, the timer is not reset due to WebSocket activity. I will get around this using the setMaxIdleTime method on my BlockingChannelConnector object to set the timeout value to Integer MAX_VALUE.

+3
Jan 30 2018-12-17T00:
source share

I think this timeout that you are experiencing is actually part of TCP / IP, and the solution is to just send empty messages from time to time.

+2
Jan 29 '12 at 20:13
source share

Here is an example of how to set the Jetty web server timeout (the most likely culprit) using WebSocketServlet (in scala, sorry, but the syntax is almost the same).

 import javax.servlet.annotation.WebServlet import org.eclipse.jetty.websocket.servlet.{WebSocketServletFactory, WebSocketServlet} @WebServlet(name = "WebSocket Servlet") class WebsocketServlet extends WebSocketServlet { override def configure(factory: WebSocketServletFactory): Unit = { factory.getPolicy.setIdleTimeout(1000 * 3600) factory.register(classOf[ClientWebsocket]) } } 
+2
Jun 30 '14 at 16:08
source share

I have a similar experience, and I believe that it could be a browser that shortens the session. I also set maxIdleTimeout, but the session disconnects independently. For me, it seems that the client (browser) is synchronizing the session and then hanging up.

I don’t know how to get around this.

+1
Feb 10 '15 at 10:40
source share



All Articles