Class WebSocketServerProtocolHandler
- java.lang.Object
-
- io.netty.channel.ChannelHandlerAdapter
-
- io.netty.channel.ChannelInboundHandlerAdapter
-
- io.netty.handler.codec.MessageToMessageDecoder<WebSocketFrame>
-
- io.netty.handler.codec.http.websocketx.WebSocketProtocolHandler
-
- io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler
-
- All Implemented Interfaces:
ChannelHandler
,ChannelInboundHandler
,ChannelOutboundHandler
public class WebSocketServerProtocolHandler extends WebSocketProtocolHandler
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.netty.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.netty.example.http.websocketx.server.WebSocketServer example. To know once a handshake was done you can intercept theChannelInboundHandler.userEventTriggered(ChannelHandlerContext, Object)
and check if the event was instance ofWebSocketServerProtocolHandler.HandshakeComplete
, the event will contain extra information about the handshake such as the request and selected subprotocol.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
WebSocketServerProtocolHandler.HandshakeComplete
The Handshake was completed successfully and the channel was upgraded to websockets.static class
WebSocketServerProtocolHandler.ServerHandshakeStateEvent
Events that are fired to notify about handshake status-
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Field Summary
Fields Modifier and Type Field Description private static AttributeKey<WebSocketServerHandshaker>
HANDSHAKER_ATTR_KEY
private WebSocketServerProtocolConfig
serverConfig
-
Constructor Summary
Constructors Constructor Description WebSocketServerProtocolHandler(WebSocketServerProtocolConfig serverConfig)
Base constructorWebSocketServerProtocolHandler(java.lang.String websocketPath)
WebSocketServerProtocolHandler(java.lang.String websocketPath, boolean checkStartsWith)
WebSocketServerProtocolHandler(java.lang.String websocketPath, boolean checkStartsWith, long handshakeTimeoutMillis)
WebSocketServerProtocolHandler(java.lang.String websocketPath, long handshakeTimeoutMillis)
WebSocketServerProtocolHandler(java.lang.String websocketPath, java.lang.String subprotocols)
WebSocketServerProtocolHandler(java.lang.String websocketPath, java.lang.String subprotocols, boolean allowExtensions)
WebSocketServerProtocolHandler(java.lang.String websocketPath, java.lang.String subprotocols, boolean checkStartsWith, boolean dropPongFrames, long handshakeTimeoutMillis, WebSocketDecoderConfig decoderConfig)
WebSocketServerProtocolHandler(java.lang.String websocketPath, java.lang.String subprotocols, boolean allowExtensions, int maxFrameSize)
WebSocketServerProtocolHandler(java.lang.String websocketPath, java.lang.String subprotocols, boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch)
WebSocketServerProtocolHandler(java.lang.String websocketPath, java.lang.String subprotocols, boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, boolean checkStartsWith)
WebSocketServerProtocolHandler(java.lang.String websocketPath, java.lang.String subprotocols, boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, boolean checkStartsWith, boolean dropPongFrames)
WebSocketServerProtocolHandler(java.lang.String websocketPath, java.lang.String subprotocols, boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, boolean checkStartsWith, boolean dropPongFrames, long handshakeTimeoutMillis)
WebSocketServerProtocolHandler(java.lang.String websocketPath, java.lang.String subprotocols, boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, boolean checkStartsWith, long handshakeTimeoutMillis)
WebSocketServerProtocolHandler(java.lang.String websocketPath, java.lang.String subprotocols, boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, long handshakeTimeoutMillis)
WebSocketServerProtocolHandler(java.lang.String websocketPath, java.lang.String subprotocols, boolean allowExtensions, int maxFrameSize, long handshakeTimeoutMillis)
WebSocketServerProtocolHandler(java.lang.String websocketPath, java.lang.String subprotocols, boolean allowExtensions, long handshakeTimeoutMillis)
WebSocketServerProtocolHandler(java.lang.String websocketPath, java.lang.String subprotocols, long handshakeTimeoutMillis)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected WebSocketServerHandshakeException
buildHandshakeException(java.lang.String message)
Returns aWebSocketHandshakeException
that depends on which client or server pipeline this handler belongs.protected void
decode(ChannelHandlerContext ctx, WebSocketFrame frame, java.util.List<java.lang.Object> out)
Decode from one message to an other.void
exceptionCaught(ChannelHandlerContext ctx, java.lang.Throwable cause)
CallsChannelHandlerContext.fireExceptionCaught(Throwable)
to forward to the nextChannelHandler
in theChannelPipeline
.(package private) static WebSocketServerHandshaker
getHandshaker(Channel channel)
void
handlerAdded(ChannelHandlerContext ctx)
Do nothing by default, sub-classes may override this method.(package private) static void
setHandshaker(Channel channel, WebSocketServerHandshaker handshaker)
-
Methods inherited from class io.netty.handler.codec.http.websocketx.WebSocketProtocolHandler
bind, close, closeSent, connect, deregister, disconnect, flush, read, write
-
Methods inherited from class io.netty.handler.codec.MessageToMessageDecoder
acceptInboundMessage, channelRead
-
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelActive, channelInactive, channelReadComplete, channelRegistered, channelUnregistered, channelWritabilityChanged, userEventTriggered
-
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, handlerRemoved, isSharable
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.netty.channel.ChannelHandler
handlerRemoved
-
-
-
-
Field Detail
-
HANDSHAKER_ATTR_KEY
private static final AttributeKey<WebSocketServerHandshaker> HANDSHAKER_ATTR_KEY
-
serverConfig
private final WebSocketServerProtocolConfig serverConfig
-
-
Constructor Detail
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(WebSocketServerProtocolConfig serverConfig)
Base constructor- Parameters:
serverConfig
- Server protocol configuration.
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(java.lang.String websocketPath)
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(java.lang.String websocketPath, long handshakeTimeoutMillis)
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(java.lang.String websocketPath, boolean checkStartsWith)
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(java.lang.String websocketPath, boolean checkStartsWith, long handshakeTimeoutMillis)
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(java.lang.String websocketPath, java.lang.String subprotocols)
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(java.lang.String websocketPath, java.lang.String subprotocols, long handshakeTimeoutMillis)
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(java.lang.String websocketPath, java.lang.String subprotocols, boolean allowExtensions)
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(java.lang.String websocketPath, java.lang.String subprotocols, boolean allowExtensions, long handshakeTimeoutMillis)
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(java.lang.String websocketPath, java.lang.String subprotocols, boolean allowExtensions, int maxFrameSize)
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(java.lang.String websocketPath, java.lang.String subprotocols, boolean allowExtensions, int maxFrameSize, long handshakeTimeoutMillis)
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(java.lang.String websocketPath, java.lang.String subprotocols, boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch)
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(java.lang.String websocketPath, java.lang.String subprotocols, boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, long handshakeTimeoutMillis)
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(java.lang.String websocketPath, java.lang.String subprotocols, boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, boolean checkStartsWith)
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(java.lang.String websocketPath, java.lang.String subprotocols, boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, boolean checkStartsWith, long handshakeTimeoutMillis)
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(java.lang.String websocketPath, java.lang.String subprotocols, boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, boolean checkStartsWith, boolean dropPongFrames)
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(java.lang.String websocketPath, java.lang.String subprotocols, boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, boolean checkStartsWith, boolean dropPongFrames, long handshakeTimeoutMillis)
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(java.lang.String websocketPath, java.lang.String subprotocols, boolean checkStartsWith, boolean dropPongFrames, long handshakeTimeoutMillis, WebSocketDecoderConfig decoderConfig)
-
-
Method Detail
-
handlerAdded
public void handlerAdded(ChannelHandlerContext ctx)
Description copied from class:ChannelHandlerAdapter
Do nothing by default, sub-classes may override this method.- Specified by:
handlerAdded
in interfaceChannelHandler
- Overrides:
handlerAdded
in classChannelHandlerAdapter
-
decode
protected void decode(ChannelHandlerContext ctx, WebSocketFrame frame, java.util.List<java.lang.Object> out) throws java.lang.Exception
Description copied from class:MessageToMessageDecoder
Decode from one message to an other. This method will be called for each written message that can be handled by this decoder.- Overrides:
decode
in classWebSocketProtocolHandler
- Parameters:
ctx
- theChannelHandlerContext
which thisMessageToMessageDecoder
belongs toframe
- the message to decode to an other oneout
- theList
to which decoded messages should be added- Throws:
java.lang.Exception
- is thrown if an error occurs
-
buildHandshakeException
protected WebSocketServerHandshakeException buildHandshakeException(java.lang.String message)
Description copied from class:WebSocketProtocolHandler
Returns aWebSocketHandshakeException
that depends on which client or server pipeline this handler belongs. Should be overridden in implementation otherwise a default exception is used.- Overrides:
buildHandshakeException
in classWebSocketProtocolHandler
-
exceptionCaught
public void exceptionCaught(ChannelHandlerContext ctx, java.lang.Throwable cause) throws java.lang.Exception
Description copied from class:ChannelInboundHandlerAdapter
CallsChannelHandlerContext.fireExceptionCaught(Throwable)
to forward to the nextChannelHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
exceptionCaught
in interfaceChannelHandler
- Specified by:
exceptionCaught
in interfaceChannelInboundHandler
- Overrides:
exceptionCaught
in classWebSocketProtocolHandler
- Throws:
java.lang.Exception
-
getHandshaker
static WebSocketServerHandshaker getHandshaker(Channel channel)
-
setHandshaker
static void setHandshaker(Channel channel, WebSocketServerHandshaker handshaker)
-
-