Fix websocket timeout

This commit is contained in:
Marc Baloup 2023-03-16 23:30:37 +01:00
parent ced9b0eaca
commit 872746b46f
2 changed files with 5 additions and 0 deletions

View File

@ -5,6 +5,7 @@ import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.WebSocketAdapter; import org.eclipse.jetty.websocket.api.WebSocketAdapter;
import java.io.IOException; import java.io.IOException;
import java.time.Duration;
/** /**
* Minimal implementation of a Websocket server endpoint using the Jetty Websocket API. * 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) public final void onWebSocketConnect(Session sess)
{ {
super.onWebSocketConnect(sess); super.onWebSocketConnect(sess);
sess.setIdleTimeout(Duration.ofDays(1000)); // practically infinite
onConnect(); onConnect();
} }

View File

@ -5,6 +5,7 @@ import fr.pandacube.lib.ws.payloads.LoginPayload;
import fr.pandacube.lib.ws.payloads.LoginSucceedPayload; import fr.pandacube.lib.ws.payloads.LoginSucceedPayload;
import fr.pandacube.lib.ws.payloads.Payload; import fr.pandacube.lib.ws.payloads.Payload;
import java.time.Duration;
import java.util.function.Supplier; import java.util.function.Supplier;
/** /**
@ -26,6 +27,7 @@ public abstract class KeyProtectedServerWS extends AbstractServerWS {
@Override @Override
public final void onConnect() { 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 // nothing, just wait for the client to login
} }
@ -37,6 +39,7 @@ public abstract class KeyProtectedServerWS extends AbstractServerWS {
else if (payload instanceof LoginPayload login) { else if (payload instanceof LoginPayload login) {
if (keySupplier.get().equals(login.key)) { if (keySupplier.get().equals(login.key)) {
loginSucceed = true; loginSucceed = true;
getSession().setIdleTimeout(Duration.ofDays(1000)); // allow infinite timeout for logged in clients
trySendAsJson(new LoginSucceedPayload()); trySendAsJson(new LoginSucceedPayload());
onLoginSucceed(); onLoginSucceed();
} }