LeoR
LeoR

Reputation: 716

Do I need to tell Apache Tomcat to keep a Websocket connection open?

I am trying the Apache Tomcat Websocket Implementation. The Problem is, that the connection always gets closed after at most 30 seconds of idle time.

Do I need to configure Tomcat so that it keeps the connection open? If so how can I do that? I tried to set up the connectionTimeout in the Connector element of the server.xml for the HTTP protocol. That had no effect, but I don't know if it just doesn't work because I didn't adjust the protocol attribute to something websocket specific (since I don't know what that protocol declaration looks like).

Or is it perhaps a client-side problem.

Thanks for helping,
Leo

edit: This Problem has been solved with Tomcat 7.0.28 (Bug Description, changelog)

Upvotes: 3

Views: 14414

Answers (5)

Badal
Badal

Reputation: 4188

apart from connectionTimeout setting in server.xml, another reason could be your websocket message implementaton class as well. getReadTimeout method is one which is used to set websocket timeout in milliseconds. for example, if you want to set socket timeout 2 minutes (2*60*1000) than you may use below code. Note: you can set infinite timeout (always open) by returning -1.

@Override
public int getReadTimeout() {
    return 2*60*1000;
}

For complete implementation, Refer this sample code in my answer. tomcat websocket servlet listening port

Upvotes: 0

Given
Given

Reputation: 775

I manage to get the websocket open infinitely. All you need to do is to set the connectionTimeout in the server.xml of Apache to minus one (connectionTimeout="-1"). That solved my problem. :) ref:

Upvotes: 4

BOUBAKEUR
BOUBAKEUR

Reputation: 1

SET IN SERVER.XML

<Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="999999"
           redirectPort="8443" />

Upvotes: 0

Pidster
Pidster

Reputation: 618

If you need to keep the connection open, ping the client/server. This is the only way to reliably ensure that there is something at the end of the connection.

The internet is not a reliable transport, so it's worth ensuring that your code can handle disconnects.

Upvotes: 1

jfarcand
jfarcand

Reputation: 1705

Tomcat has several issues with WebSocket, and one of them is it close the connection after 20 seconds (this is the connectTimeout value in server.xml). Increase that value to workaround the issue.

-- Jeanfrancois

Upvotes: 4

Related Questions