Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetty 9 RC2 websocket timeout

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

like image 526
fso Avatar asked Jun 06 '26 21:06

fso


1 Answers

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)
    {
    }
}
like image 123
Joakim Erdfelt Avatar answered Jun 09 '26 16:06

Joakim Erdfelt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!