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!
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).
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.
However, the connection between a client and your WebSocket app closes when no traffic is sent between them for 60 seconds.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With