Top Description Inners Fields Constructors Methods
io.netty.handler.codec.http.websocketx

public Class WebSocketServerProtocolHandler

extends WebSocketProtocolHandler
Class Inheritance
Imports
io.netty.buffer.Unpooled, io.netty.channel.Channel, .ChannelFutureListener, .ChannelHandlerContext, .ChannelInboundHandler, .ChannelPipeline, .ChannelPromise, io.netty.handler.codec.http.DefaultFullHttpResponse, .FullHttpResponse, .HttpHeaders, .HttpResponseStatus, io.netty.util.AttributeKey, java.util.List

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 HandshakeComplete, the event will contain extra information about the handshake such as the request and selected subprotocol.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
WebSocketServerProtocolHandler.HandshakeComplete

The Handshake was completed successfully and the channel was upgraded to websockets.

public static enum
WebSocketServerProtocolHandler.ServerHandshakeStateEvent

Events that are fired to notify about handshake status

Field Summary

Modifier and TypeField and Description
private static final AttributeKey<WebSocketServerHandshaker>
private final WebSocketServerProtocolConfig

Constructor Summary

AccessConstructor and Description
public
WebSocketServerProtocolHandler(WebSocketServerProtocolConfig
Server protocol configuration.
serverConfig
)

Base constructor

public
public
WebSocketServerProtocolHandler(String websocketPath, long handshakeTimeoutMillis)

public
WebSocketServerProtocolHandler(String websocketPath, boolean checkStartsWith)

public
WebSocketServerProtocolHandler(String websocketPath, boolean checkStartsWith, long handshakeTimeoutMillis)

public
WebSocketServerProtocolHandler(String websocketPath, String subprotocols)

public
WebSocketServerProtocolHandler(String websocketPath, String subprotocols, long handshakeTimeoutMillis)

public
WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions)

public
WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, long handshakeTimeoutMillis)

public
WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, int maxFrameSize)

public
WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, int maxFrameSize, long handshakeTimeoutMillis)

public
WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch)

public
WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, long handshakeTimeoutMillis)

public
WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, boolean checkStartsWith)

public
WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, boolean checkStartsWith, long handshakeTimeoutMillis)

public
WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, boolean checkStartsWith, boolean dropPongFrames)

public
WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, boolean checkStartsWith, boolean dropPongFrames, long handshakeTimeoutMillis)

public
WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean checkStartsWith, boolean dropPongFrames, long handshakeTimeoutMillis, WebSocketDecoderConfig decoderConfig)

Method Summary

Modifier and TypeMethod and Description
protected WebSocketServerHandshakeException
buildHandshakeException(String message)

Overrides io.netty.handler.codec.http.websocketx.WebSocketProtocolHandler.buildHandshakeException.

Returns a WebSocketHandshakeException that depends on which client or server pipeline this handler belongs.
protected void
decode(ChannelHandlerContext ctx, WebSocketFrame
the message to decode to an other one
frame
,
List<Object>
the List to which decoded messages should be added
out
)

Overrides io.netty.handler.codec.http.websocketx.WebSocketProtocolHandler.decode.

Decode from one message to an other.
public void
pack-priv static WebSocketServerHandshaker
public void
handlerAdded(ChannelHandlerContext ctx)

Overrides io.netty.channel.ChannelHandlerAdapter.handlerAdded.

Implements io.netty.channel.ChannelHandler.handlerAdded.

Gets called after the ChannelHandler was added to the actual context and it's ready to handle events.
pack-priv static void
Inherited from io.netty.handler.codec.http.websocketx.WebSocketProtocolHandler:
bindclosecloseSentconnectderegisterdisconnectflushreadwrite

Field Detail

HANDSHAKER_ATTR_KEYback to summary
private static final AttributeKey<WebSocketServerHandshaker> HANDSHAKER_ATTR_KEY
serverConfigback to summary
private final WebSocketServerProtocolConfig serverConfig

Constructor Detail

WebSocketServerProtocolHandlerback to summary
public WebSocketServerProtocolHandler(WebSocketServerProtocolConfig serverConfig)

Base constructor

Parameters
serverConfig:WebSocketServerProtocolConfig

Server protocol configuration.

WebSocketServerProtocolHandlerback to summary
public WebSocketServerProtocolHandler(String websocketPath)
WebSocketServerProtocolHandlerback to summary
public WebSocketServerProtocolHandler(String websocketPath, long handshakeTimeoutMillis)
WebSocketServerProtocolHandlerback to summary
public WebSocketServerProtocolHandler(String websocketPath, boolean checkStartsWith)
WebSocketServerProtocolHandlerback to summary
public WebSocketServerProtocolHandler(String websocketPath, boolean checkStartsWith, long handshakeTimeoutMillis)
WebSocketServerProtocolHandlerback to summary
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols)
WebSocketServerProtocolHandlerback to summary
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, long handshakeTimeoutMillis)
WebSocketServerProtocolHandlerback to summary
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions)
WebSocketServerProtocolHandlerback to summary
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, long handshakeTimeoutMillis)
WebSocketServerProtocolHandlerback to summary
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, int maxFrameSize)
WebSocketServerProtocolHandlerback to summary
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, int maxFrameSize, long handshakeTimeoutMillis)
WebSocketServerProtocolHandlerback to summary
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch)
WebSocketServerProtocolHandlerback to summary
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, long handshakeTimeoutMillis)
WebSocketServerProtocolHandlerback to summary
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, boolean checkStartsWith)
WebSocketServerProtocolHandlerback to summary
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, boolean checkStartsWith, long handshakeTimeoutMillis)
WebSocketServerProtocolHandlerback to summary
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, boolean checkStartsWith, boolean dropPongFrames)
WebSocketServerProtocolHandlerback to summary
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, boolean checkStartsWith, boolean dropPongFrames, long handshakeTimeoutMillis)
WebSocketServerProtocolHandlerback to summary
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean checkStartsWith, boolean dropPongFrames, long handshakeTimeoutMillis, WebSocketDecoderConfig decoderConfig)

Method Detail

buildHandshakeExceptionback to summary
protected WebSocketServerHandshakeException buildHandshakeException(String message)

Overrides io.netty.handler.codec.http.websocketx.WebSocketProtocolHandler.buildHandshakeException.

Doc from io.netty.handler.codec.http.websocketx.WebSocketProtocolHandler.buildHandshakeException.

Returns a WebSocketHandshakeException that depends on which client or server pipeline this handler belongs. Should be overridden in implementation otherwise a default exception is used.

Annotations
@Override
decodeback to summary
protected void decode(ChannelHandlerContext ctx, WebSocketFrame frame, List<Object> out) throws Exception

Overrides io.netty.handler.codec.http.websocketx.WebSocketProtocolHandler.decode.

Doc from io.netty.handler.codec.MessageToMessageDecoder.decode.

Decode from one message to an other. This method will be called for each written message that can be handled by this decoder.

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext which this MessageToMessageDecoder belongs to

frame:WebSocketFrame

the message to decode to an other one

out:List<Object>

the List to which decoded messages should be added

Annotations
@Override
Exceptions
Exception:
is thrown if an error occurs
exceptionCaughtback to summary
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception

Overrides io.netty.handler.codec.http.websocketx.WebSocketProtocolHandler.exceptionCaught.

Implements io.netty.channel.ChannelHandler.exceptionCaught, io.netty.channel.ChannelInboundHandler.exceptionCaught.

Doc from io.netty.channel.ChannelHandler.exceptionCaught.

Gets called if a Throwable was thrown.

Annotations
@Override
getHandshakerback to summary
pack-priv static WebSocketServerHandshaker getHandshaker(Channel channel)
handlerAddedback to summary
public void handlerAdded(ChannelHandlerContext ctx)

Overrides io.netty.channel.ChannelHandlerAdapter.handlerAdded.

Implements io.netty.channel.ChannelHandler.handlerAdded.

Doc from io.netty.channel.ChannelHandler.handlerAdded.

Gets called after the ChannelHandler was added to the actual context and it's ready to handle events.

Annotations
@Override
setHandshakerback to summary
pack-priv static void setHandshaker(Channel channel, WebSocketServerHandshaker handshaker)
io.netty.handler.codec.http.websocketx back to summary

public final Class WebSocketServerProtocolHandler.HandshakeComplete

extends Object
Class Inheritance
  • java.lang.Object
  • io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler.HandshakeComplete

The Handshake was completed successfully and the channel was upgraded to websockets.

Field Summary

Modifier and TypeField and Description
private final HttpHeaders
private final String
private final String

Constructor Summary

AccessConstructor and Description
public
HandshakeComplete(String requestUri, HttpHeaders requestHeaders, String selectedSubprotocol)

Method Summary

Modifier and TypeMethod and Description
public HttpHeaders
public String
public String
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

requestHeadersback to summary
private final HttpHeaders requestHeaders
requestUriback to summary
private final String requestUri
selectedSubprotocolback to summary
private final String selectedSubprotocol

Constructor Detail

HandshakeCompleteback to summary
public HandshakeComplete(String requestUri, HttpHeaders requestHeaders, String selectedSubprotocol)

Method Detail

requestHeadersback to summary
public HttpHeaders requestHeaders()
requestUriback to summary
public String requestUri()
selectedSubprotocolback to summary
public String selectedSubprotocol()
io.netty.handler.codec.http.websocketx back to summary

public final Enum WebSocketServerProtocolHandler.ServerHandshakeStateEvent

extends Enum<WebSocketServerProtocolHandler.ServerHandshakeStateEvent>
Class Inheritance

Events that are fired to notify about handshake status

Field Summary

Modifier and TypeField and Description
public static final WebSocketServerProtocolHandler.ServerHandshakeStateEvent
HANDSHAKE_COMPLETE

The Handshake was completed successfully and the channel was upgraded to websockets.

public static final WebSocketServerProtocolHandler.ServerHandshakeStateEvent
HANDSHAKE_TIMEOUT

The Handshake was timed out

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static WebSocketServerProtocolHandler.ServerHandshakeStateEvent
public static WebSocketServerProtocolHandler.ServerHandshakeStateEvent[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

HANDSHAKE_COMPLETEback to summary
public static final WebSocketServerProtocolHandler.ServerHandshakeStateEvent HANDSHAKE_COMPLETE

Deprecated

in favor of HandshakeComplete class, it provides extra information about the handshake

The Handshake was completed successfully and the channel was upgraded to websockets.

Annotations
@Deprecated
HANDSHAKE_TIMEOUTback to summary
public static final WebSocketServerProtocolHandler.ServerHandshakeStateEvent HANDSHAKE_TIMEOUT

The Handshake was timed out

Constructor Detail

ServerHandshakeStateEventback to summary
private ServerHandshakeStateEvent()

Method Detail

valueOfback to summary
public static WebSocketServerProtocolHandler.ServerHandshakeStateEvent valueOf(String name)
valuesback to summary
public static WebSocketServerProtocolHandler.ServerHandshakeStateEvent[] values()