Package io.muserver
Class WebSocketHandlerBuilder
- java.lang.Object
-
- io.muserver.WebSocketHandlerBuilder
-
- All Implemented Interfaces:
MuHandlerBuilder<WebSocketHandler>
public class WebSocketHandlerBuilder extends java.lang.Object implements MuHandlerBuilder<WebSocketHandler>
Used to create handlers for web sockets.
-
-
Constructor Summary
Constructors Constructor Description WebSocketHandlerBuilder()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description WebSocketHandler
build()
Creates the websocket handler.static WebSocketHandlerBuilder
webSocketHandler()
Creates a new handler builder.static WebSocketHandlerBuilder
webSocketHandler(MuWebSocketFactory factory)
Creates a new handler builder with the given factory.WebSocketHandlerBuilder
withIdleReadTimeout(long duration, java.util.concurrent.TimeUnit unit)
Sets the idle timeout.WebSocketHandlerBuilder
withMaxFramePayloadLength(int maxFramePayloadLength)
Sets the maximum size in bytes that a frame can be.WebSocketHandlerBuilder
withPath(java.lang.String path)
Sets the path to listen on for this handler.WebSocketHandlerBuilder
withPingSentAfterNoWritesFor(int duration, java.util.concurrent.TimeUnit unit)
Sets the amount of time to wait before sending a ping message if no messages having been sent.WebSocketHandlerBuilder
withWebSocketFactory(MuWebSocketFactory factory)
Sets the factory that decides whether to create a websocket connection for a request.
-
-
-
Method Detail
-
withWebSocketFactory
public WebSocketHandlerBuilder withWebSocketFactory(MuWebSocketFactory factory)
Sets the factory that decides whether to create a websocket connection for a request.
Note that the factory will only be called if the request is a websocket upgrade request.
- Parameters:
factory
- A factory that creates websockets, or returns null if the websocket connection shouldn't be created.- Returns:
- This builder
-
withPath
public WebSocketHandlerBuilder withPath(java.lang.String path)
Sets the path to listen on for this handler. Note that an alternative to setting a path is to look atMuRequest.uri()
in theMuWebSocketFactory
and decide whether to handle the request or not.Note: if this handler is nested within a
ContextHandler
, then this is the relative path.- Parameters:
path
- The path of this web socket endpoint.- Returns:
- This builder
-
withIdleReadTimeout
public WebSocketHandlerBuilder withIdleReadTimeout(long duration, java.util.concurrent.TimeUnit unit)
Sets the idle timeout. If no messages are received within this time then the connection is closed.The default is 5 minutes.
- Parameters:
duration
- The allowed timeout duration, or 0 to disable timeouts.unit
- The unit of the duration.- Returns:
- This builder
-
withPingSentAfterNoWritesFor
public WebSocketHandlerBuilder withPingSentAfterNoWritesFor(int duration, java.util.concurrent.TimeUnit unit)
Sets the amount of time to wait before sending a ping message if no messages having been sent.The default is 30 seconds.
- Parameters:
duration
- The allowed timeout duration, or 0 to disable timeouts.unit
- The unit of the duration.- Returns:
- This builder
-
withMaxFramePayloadLength
public WebSocketHandlerBuilder withMaxFramePayloadLength(int maxFramePayloadLength)
Sets the maximum size in bytes that a frame can be. Defaults to65536
- Parameters:
maxFramePayloadLength
- The maximum allowed size in bytes of websocket frames.- Returns:
- This builder
-
build
public WebSocketHandler build()
Creates the websocket handler.- Specified by:
build
in interfaceMuHandlerBuilder<WebSocketHandler>
- Returns:
- A websocket handler
-
webSocketHandler
public static WebSocketHandlerBuilder webSocketHandler()
Creates a new handler builder.- Returns:
- A new handler builder
-
webSocketHandler
public static WebSocketHandlerBuilder webSocketHandler(MuWebSocketFactory factory)
Creates a new handler builder with the given factory.- Parameters:
factory
- The factory to use.- Returns:
- A new handler builder.
-
-