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.WebSocketServerProtocolHandler
- All Implemented Interfaces:
io.netty.channel.ChannelHandler,io.netty.channel.ChannelInboundHandler,io.netty.channel.ChannelOutboundHandler
public class WebSocketServerProtocolHandler
extends io.netty.handler.codec.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.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 the
ChannelInboundHandler.userEventTriggered(ChannelHandlerContext, Object) and check if the event was instance
of WebSocketServerProtocolHandler.HandshakeComplete, the event will contain extra information about the handshake such as the request and
selected subprotocol.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classThe Handshake was completed successfully and the channel was upgraded to websockets.static enumEvents that are fired to notify about handshake statusNested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
io.netty.channel.ChannelHandler.Sharable -
Constructor Summary
ConstructorsConstructorDescriptionWebSocketServerProtocolHandler(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
Modifier and TypeMethodDescriptionvoidbind(io.netty.channel.ChannelHandlerContext ctx, SocketAddress localAddress, io.netty.channel.ChannelPromise promise) protected WebSocketServerHandshakeExceptionbuildHandshakeException(String message) Returns aWebSocketHandshakeExceptionthat depends on which client or server pipeline this handler belongs.voidclose(io.netty.channel.ChannelHandlerContext ctx, io.netty.channel.ChannelPromise promise) voidconnect(io.netty.channel.ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, io.netty.channel.ChannelPromise promise) protected voiddecode(io.netty.channel.ChannelHandlerContext ctx, WebSocketFrame frame, List<Object> out) voidderegister(io.netty.channel.ChannelHandlerContext ctx, io.netty.channel.ChannelPromise promise) voiddisconnect(io.netty.channel.ChannelHandlerContext ctx, io.netty.channel.ChannelPromise promise) voidexceptionCaught(io.netty.channel.ChannelHandlerContext ctx, Throwable cause) voidflush(io.netty.channel.ChannelHandlerContext ctx) voidhandlerAdded(io.netty.channel.ChannelHandlerContext ctx) voidread(io.netty.channel.ChannelHandlerContext ctx) voidwrite(io.netty.channel.ChannelHandlerContext ctx, Object msg, io.netty.channel.ChannelPromise promise) Methods inherited from class io.netty.handler.codec.MessageToMessageDecoder
acceptInboundMessage, channelReadMethods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelActive, channelInactive, channelReadComplete, channelRegistered, channelUnregistered, channelWritabilityChanged, userEventTriggeredMethods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, handlerRemoved, isSharableMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.netty.channel.ChannelHandler
handlerRemoved
-
Constructor Details
-
WebSocketServerProtocolHandler
Base constructor- Parameters:
serverConfig- Server protocol configuration.
-
WebSocketServerProtocolHandler
-
WebSocketServerProtocolHandler
-
WebSocketServerProtocolHandler
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(String websocketPath, boolean checkStartsWith, long handshakeTimeoutMillis) -
WebSocketServerProtocolHandler
-
WebSocketServerProtocolHandler
-
WebSocketServerProtocolHandler
-
WebSocketServerProtocolHandler
-
WebSocketServerProtocolHandler
-
WebSocketServerProtocolHandler
-
WebSocketServerProtocolHandler
-
WebSocketServerProtocolHandler
-
WebSocketServerProtocolHandler
-
WebSocketServerProtocolHandler
-
WebSocketServerProtocolHandler
-
WebSocketServerProtocolHandler
-
WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean checkStartsWith, boolean dropPongFrames, long handshakeTimeoutMillis, WebSocketDecoderConfig decoderConfig)
-
-
Method Details
-
handlerAdded
public void handlerAdded(io.netty.channel.ChannelHandlerContext ctx) - Specified by:
handlerAddedin interfaceio.netty.channel.ChannelHandler- Overrides:
handlerAddedin classio.netty.channel.ChannelHandlerAdapter
-
decode
protected void decode(io.netty.channel.ChannelHandlerContext ctx, WebSocketFrame frame, List<Object> out) throws Exception - Throws:
Exception
-
buildHandshakeException
Returns aWebSocketHandshakeExceptionthat depends on which client or server pipeline this handler belongs. Should be overridden in implementation otherwise a default exception is used. -
exceptionCaught
public void exceptionCaught(io.netty.channel.ChannelHandlerContext ctx, Throwable cause) throws Exception - Specified by:
exceptionCaughtin interfaceio.netty.channel.ChannelHandler- Specified by:
exceptionCaughtin interfaceio.netty.channel.ChannelInboundHandler- Throws:
Exception
-
close
public void close(io.netty.channel.ChannelHandlerContext ctx, io.netty.channel.ChannelPromise promise) throws Exception - Specified by:
closein interfaceio.netty.channel.ChannelOutboundHandler- Throws:
Exception
-
write
public void write(io.netty.channel.ChannelHandlerContext ctx, Object msg, io.netty.channel.ChannelPromise promise) throws Exception - Specified by:
writein interfaceio.netty.channel.ChannelOutboundHandler- Throws:
Exception
-
bind
public void bind(io.netty.channel.ChannelHandlerContext ctx, SocketAddress localAddress, io.netty.channel.ChannelPromise promise) throws Exception - Specified by:
bindin interfaceio.netty.channel.ChannelOutboundHandler- Throws:
Exception
-
connect
public void connect(io.netty.channel.ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, io.netty.channel.ChannelPromise promise) throws Exception - Specified by:
connectin interfaceio.netty.channel.ChannelOutboundHandler- Throws:
Exception
-
disconnect
public void disconnect(io.netty.channel.ChannelHandlerContext ctx, io.netty.channel.ChannelPromise promise) throws Exception - Specified by:
disconnectin interfaceio.netty.channel.ChannelOutboundHandler- Throws:
Exception
-
deregister
public void deregister(io.netty.channel.ChannelHandlerContext ctx, io.netty.channel.ChannelPromise promise) throws Exception - Specified by:
deregisterin interfaceio.netty.channel.ChannelOutboundHandler- Throws:
Exception
-
read
- Specified by:
readin interfaceio.netty.channel.ChannelOutboundHandler- Throws:
Exception
-
flush
- Specified by:
flushin interfaceio.netty.channel.ChannelOutboundHandler- Throws:
Exception
-