Module io.netty5.codec.http
Class WebSocketServerProtocolHandler
- java.lang.Object
-
- io.netty5.channel.ChannelHandlerAdapter
-
- io.netty5.handler.codec.MessageToMessageDecoder<WebSocketFrame>
-
- io.netty5.handler.codec.http.websocketx.WebSocketServerProtocolHandler
-
- All Implemented Interfaces:
ChannelHandler
public class WebSocketServerProtocolHandler extends MessageToMessageDecoder<WebSocketFrame>
This handler does all the heavy lifting for you to run a websocket server. It takes care of websocket handshaking as well as processing of control frames (Close, Ping, Pong). Text and Binary data frames are passed to the next handler in the pipeline (implemented by you) for processing. See io.netty5.example.http.websocketx.html5.WebSocketServer for usage. The implementation of this handler assumes that you just want to run a websocket server and not process other types HTTP requests (like GET and POST). If you wish to support both HTTP requests and websockets in the one server, refer to the io.netty5.example.http.websocketx.server.WebSocketServer example. To know once a handshake was done you can intercept theChannelHandler.channelInboundEvent(ChannelHandlerContext, Object)
and check if the event was instance ofWebSocketServerHandshakeCompletionEvent
, the event will contain extra information about the handshake such as the request and selected subprotocol.
-
-
Constructor Summary
Constructors Constructor Description WebSocketServerProtocolHandler(WebSocketServerProtocolConfig serverConfig)
Base constructorWebSocketServerProtocolHandler(String websocketPath)
WebSocketServerProtocolHandler(String websocketPath, boolean checkStartsWith)
WebSocketServerProtocolHandler(String websocketPath, boolean checkStartsWith, long handshakeTimeoutMillis)
WebSocketServerProtocolHandler(String websocketPath, long handshakeTimeoutMillis)
WebSocketServerProtocolHandler(String websocketPath, String subprotocols)
WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions)
WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean checkStartsWith, boolean dropPongFrames, long handshakeTimeoutMillis, WebSocketDecoderConfig decoderConfig)
WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, int maxFrameSize)
WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch)
WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, boolean checkStartsWith)
WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, boolean checkStartsWith, boolean dropPongFrames)
WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, boolean checkStartsWith, boolean dropPongFrames, long handshakeTimeoutMillis)
WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, boolean checkStartsWith, long handshakeTimeoutMillis)
WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, long handshakeTimeoutMillis)
WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, int maxFrameSize, long handshakeTimeoutMillis)
WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, long handshakeTimeoutMillis)
WebSocketServerProtocolHandler(String websocketPath, String subprotocols, long handshakeTimeoutMillis)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected WebSocketServerHandshakeException
buildHandshakeException(String message)
Returns aWebSocketHandshakeException
that depends on which client or server pipeline this handler belongs.void
channelExceptionCaught(ChannelHandlerContext ctx, Throwable cause)
Gets called if aThrowable
was thrown when handling inbound events.Future<Void>
close(ChannelHandlerContext ctx)
Called once a close operation is made.protected void
decode(ChannelHandlerContext ctx, WebSocketFrame msg)
Decode from one message to another.protected void
decodeAndClose(ChannelHandlerContext ctx, WebSocketFrame frame)
Decode from one message to another.void
handlerAdded(ChannelHandlerContext ctx)
Gets called after theChannelHandler
was added to the actual context and it's ready to handle events.Future<Void>
write(ChannelHandlerContext ctx, Object msg)
Called once a write operation is made.-
Methods inherited from class io.netty5.handler.codec.MessageToMessageDecoder
acceptInboundMessage, channelRead
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.netty5.channel.ChannelHandler
bind, channelActive, channelInactive, channelInboundEvent, channelReadComplete, channelRegistered, channelShutdown, channelUnregistered, channelWritabilityChanged, connect, deregister, disconnect, flush, handlerRemoved, isSharable, pendingOutboundBytes, read, register, sendOutboundEvent, shutdown
-
-
-
-
Constructor Detail
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(WebSocketServerProtocolConfig serverConfig)
Base constructor- Parameters:
serverConfig
- Server protocol configuration.
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(String websocketPath)
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(String websocketPath, long handshakeTimeoutMillis)
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(String websocketPath, boolean checkStartsWith)
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(String websocketPath, boolean checkStartsWith, long handshakeTimeoutMillis)
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols)
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, long handshakeTimeoutMillis)
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions)
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, long handshakeTimeoutMillis)
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, int maxFrameSize)
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, int maxFrameSize, long handshakeTimeoutMillis)
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch)
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, long handshakeTimeoutMillis)
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, boolean checkStartsWith)
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, boolean checkStartsWith, long handshakeTimeoutMillis)
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, boolean checkStartsWith, boolean dropPongFrames)
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, boolean checkStartsWith, boolean dropPongFrames, long handshakeTimeoutMillis)
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean checkStartsWith, boolean dropPongFrames, long handshakeTimeoutMillis, WebSocketDecoderConfig decoderConfig)
-
-
Method Detail
-
handlerAdded
public void handlerAdded(ChannelHandlerContext ctx)
Description copied from interface:ChannelHandler
Gets called after theChannelHandler
was added to the actual context and it's ready to handle events.
-
decodeAndClose
protected void decodeAndClose(ChannelHandlerContext ctx, WebSocketFrame frame) throws Exception
Description copied from class:MessageToMessageDecoder
Decode from one message to another. This method will be called for each written message that can be handled by this decoder.The message will not be automatically disposed of after this call. Instead, the responsibility of ensuring that messages are disposed of falls upon the implementor of this method.
Subclasses that wish to have incoming messages automatically disposed of should instead override the
MessageToMessageDecoder.decodeAndClose(ChannelHandlerContext, Object)
method.- Parameters:
ctx
- theChannelHandlerContext
which thisMessageToMessageDecoder
belongs toframe
- the message to decode to another one- Throws:
Exception
- is thrown if an error occurs
-
buildHandshakeException
protected WebSocketServerHandshakeException buildHandshakeException(String message)
Returns aWebSocketHandshakeException
that depends on which client or server pipeline this handler belongs. Should be overridden in implementation otherwise a default exception is used.
-
channelExceptionCaught
public void channelExceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception
Description copied from interface:ChannelHandler
Gets called if aThrowable
was thrown when handling inbound events.- Specified by:
channelExceptionCaught
in interfaceChannelHandler
- Throws:
Exception
-
decode
protected void decode(ChannelHandlerContext ctx, WebSocketFrame msg) throws Exception
Description copied from class:MessageToMessageDecoder
Decode from one message to another. This method will be called for each written message that can be handled by this decoder.The message will be disposed of after this call.
Subclasses that wish to sometimes pass messages through, should instead override the
MessageToMessageDecoder.decodeAndClose(ChannelHandlerContext, Object)
method.- Overrides:
decode
in classMessageToMessageDecoder<WebSocketFrame>
- Parameters:
ctx
- theChannelHandlerContext
which thisMessageToMessageDecoder
belongs tomsg
- the message to decode to another one- Throws:
Exception
- is thrown if an error occurs
-
close
public Future<Void> close(ChannelHandlerContext ctx)
Description copied from interface:ChannelHandler
Called once a close operation is made.- Parameters:
ctx
- theChannelHandlerContext
for which the close operation is made- Returns:
- the
Future
which will be notified once the operation completes.
-
write
public Future<Void> write(ChannelHandlerContext ctx, Object msg)
Description copied from interface:ChannelHandler
Called once a write operation is made. The write operation will write the messages through theChannelPipeline
. Those are then ready to be flushed to the actualChannel
onceChannel.flush()
is called.- Parameters:
ctx
- theChannelHandlerContext
for which the write operation is mademsg
- the message to write- Returns:
- the
Future
which will be notified once the operation completes.
-
-