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 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();
}

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.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();
}