WebSocket Session closes after not receiving input for some time
Asked Answered
P

1

6

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!

Potage answered 29/6, 2015 at 17:26 Comment(2)
2 ideas, first one check it from a different environment to rule out lower level network issues.Seringapatam
Second one: Test with another server to rule out server side issues, one without the api token.Seringapatam
M
1

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)

Metaphysical answered 9/7, 2015 at 5:17 Comment(1)
I tried this already, I even printed out the maxIdleTimeout of the Session to see if it was being overridden at some point, and it wasn't.Potage

© 2022 - 2024 — McMap. All rights reserved.