Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebSocket Session closes after not receiving input for some time

So I have a Websocket Session that reads information from a server. However if I cut off the information that it's receiving completely, after about a minute or so it will stop receiving new information, and won't do anything, even when the output from the server is turned back on.

I thought that the WebSocketContainer method

setDefaultMaxSessionIdleTimeout(Long time)

would fix my issue, so I set it to

container.setDefaultMaxSessionIdleTimeout(86400000L);

which I thought would mean it will continue running up to 1 day of inactivity.

However this is not the case, it stops after just a minute of inactivity. Below is the code I'm using, maybe someone can let me know what I'm doing wrong:

public void run(String... args) throws Exception {
    log.info("Starting...");
    log.info("-- API URL:  {}", apiUrl);
    log.info("-- API Token:  {}", apiToken);
    WebSocketContainer container = ContainerProvider.getWebSocketContainer();
    container.setDefaultMaxSessionIdleTimeout(86400000L);
    ClientEndpointConfig config = ClientEndpointConfig
            .Builder.create()
            .configurator(new CustomConfigurator(apiToken))
            .build();
    try {
        session = container.connectToServer(ConsumerClient.class, config, URI.create(apiUrl));
    } catch (DeploymentException de) {
        log.error("Failed to connect - DeploymentException:", de);
    } catch (IOException ioe) {
        log.error("IOException:", ioe);
    }
    if (this.session == null) {
        throw new RuntimeException("Unable to connect to endpoint.");
    }

    log.info("Max Idle Timeout: " + session.getMaxIdleTimeout());

    log.info("Connected.");
    log.info("Type \"exit\" to cancel.");
    log.info("...Waiting for data...");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String input;
    try {
        do {
            input = br.readLine();
            if (!input.equals("exit")) {
                this.session.getBasicRemote().sendText(input);
            }
        } while (!input.equals("exit"));
    } catch (IOException e) {
        log.error("IOException:", e);
    }
}

I'm fairly new to websockets so I may be completely misunderstanding something, but I hope someone will be able to point me in the right direction. Thanks in advance!

like image 730
shadowarcher Avatar asked Jun 29 '15 17:06

shadowarcher


People also ask

What causes a WebSocket to close?

This is likely due to client security rules in the WebSocket spec to prevent abusing WebSocket. (such as using it to scan for open ports on a destination server, or for generating lots of connections for a denial-of-service attack).

Does a WebSocket timeout?

A WebSocket times out if no read or write activity occurs and no Ping messages are received within the configured timeout period. The container enforces a 30-second timeout period as the default. If the timeout period is set to -1 , no timeout period is set for the connection.

How long will a WebSocket stay open?

However, the connection between a client and your WebSocket app closes when no traffic is sent between them for 60 seconds.


1 Answers

Can you try setMaxIdleTimeout(0) on the session?

According to the setDefaultMaxSessionIdleTimeout docs:

The value may be overridden on a per session basis using Session.setMaxIdleTimeout(long)

like image 107
Buddy Avatar answered Sep 30 '22 17:09

Buddy