fso
fso

Reputation: 154

Jetty 9 RC2 websocket timeout

See https://stackoverflow.com/questions/41810306/appointment-scheduling....

Upvotes: 2

Views: 8271

Answers (1)

Joakim Erdfelt
Joakim Erdfelt

Reputation: 49452

The support for Sesssion.setIdleTimeout(long ms) was added recently to support JSR-356 (javax.websocket) work we are currently doing.

However, with 9.0.0.RC2 you can do the following to set idle timeout early, before the Session is created (this is being fixed, hopefully will make it into RC3)

Server Side option A: WebSocketServlet init-param

In your WEB-INF/web.xml for your websocket servlet, specify the following init-param

<init-param>
  <param-name>maxIdleTime</param-name>
  <param-value>10000</param-value>
</init-param>

Server Side option B: As policy change on WebSocketFactory

In your WebSocketServlet.configure(WebSocketServletFactory factory) call

@Override
public void configure(WebSocketServletFactory factory)
{
    factory.getPolicy().setIdleTimeout(10000);
}

Client Side option A: As WebSocketClient setting

WebSocketClient client = new WebSocketClient();
client.getPolicy().setIdleTimeout(10000);
client.start();

Annotated @WebSocket option

This will work for server or client websockets.

Note: you cannot mix WebSocketListener and @WebSocket annotations together

import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketError;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;

@WebSocket(maxIdleTime=10000)
public class MySocket
{
    @OnWebSocketClose
    public void onClose(int statusCode, String reason)
    {
    }

    @OnWebSocketConnect
    public void onConnect(Session sess)
    {
    }

    @OnWebSocketError
    public void onError(Throwable cause)
    {
    }

    @OnWebSocketMessage
    public void onText(String message)
    {
    }
}

Upvotes: 10

Related Questions