From 872746b46fafdd4d155b2652a60d754a6d59c092 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Thu, 16 Mar 2023 23:30:37 +0100 Subject: [PATCH] Fix websocket timeout --- .../main/java/fr/pandacube/lib/ws/server/AbstractServerWS.java | 2 ++ .../java/fr/pandacube/lib/ws/server/KeyProtectedServerWS.java | 3 +++ 2 files changed, 5 insertions(+) diff --git a/pandalib-ws-server/src/main/java/fr/pandacube/lib/ws/server/AbstractServerWS.java b/pandalib-ws-server/src/main/java/fr/pandacube/lib/ws/server/AbstractServerWS.java index b48ef63..ada1a39 100644 --- a/pandalib-ws-server/src/main/java/fr/pandacube/lib/ws/server/AbstractServerWS.java +++ b/pandalib-ws-server/src/main/java/fr/pandacube/lib/ws/server/AbstractServerWS.java @@ -5,6 +5,7 @@ import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.api.WebSocketAdapter; import java.io.IOException; +import java.time.Duration; /** * Minimal implementation of a Websocket server endpoint using the Jetty Websocket API. @@ -15,6 +16,7 @@ public abstract class AbstractServerWS extends WebSocketAdapter implements Abstr public final void onWebSocketConnect(Session sess) { super.onWebSocketConnect(sess); + sess.setIdleTimeout(Duration.ofDays(1000)); // practically infinite onConnect(); } diff --git a/pandalib-ws-server/src/main/java/fr/pandacube/lib/ws/server/KeyProtectedServerWS.java b/pandalib-ws-server/src/main/java/fr/pandacube/lib/ws/server/KeyProtectedServerWS.java index dfefe84..56ef039 100644 --- a/pandalib-ws-server/src/main/java/fr/pandacube/lib/ws/server/KeyProtectedServerWS.java +++ b/pandalib-ws-server/src/main/java/fr/pandacube/lib/ws/server/KeyProtectedServerWS.java @@ -5,6 +5,7 @@ import fr.pandacube.lib.ws.payloads.LoginPayload; import fr.pandacube.lib.ws.payloads.LoginSucceedPayload; import fr.pandacube.lib.ws.payloads.Payload; +import java.time.Duration; import java.util.function.Supplier; /** @@ -26,6 +27,7 @@ public abstract class KeyProtectedServerWS extends AbstractServerWS { @Override public final void onConnect() { + getSession().setIdleTimeout(Duration.ofSeconds(10)); // lower idle timeout for not yet logged in clients // nothing, just wait for the client to login } @@ -37,6 +39,7 @@ public abstract class KeyProtectedServerWS extends AbstractServerWS { else if (payload instanceof LoginPayload login) { if (keySupplier.get().equals(login.key)) { loginSucceed = true; + getSession().setIdleTimeout(Duration.ofDays(1000)); // allow infinite timeout for logged in clients trySendAsJson(new LoginSucceedPayload()); onLoginSucceed(); }