Class WebSocketClientProtocolHandler
- java.lang.Object
-
- io.netty.channel.ChannelHandlerAdapter
-
- io.netty.channel.ChannelInboundHandlerAdapter
-
- io.netty.handler.codec.MessageToMessageDecoder<WebSocketFrame>
-
- io.netty.handler.codec.http.websocketx.WebSocketClientProtocolHandler
-
- All Implemented Interfaces:
io.netty.channel.ChannelHandler,io.netty.channel.ChannelInboundHandler,io.netty.channel.ChannelOutboundHandler
public class WebSocketClientProtocolHandler extends io.netty.handler.codec.MessageToMessageDecoder<WebSocketFrame>
This handler does all the heavy lifting for you to run a websocket client. It takes care of websocket handshaking as well as processing of Ping, Pong frames. Text and Binary data frames are passed to the next handler in the pipeline (implemented by you) for processing. Also the close frame is passed to the next handler as you may want inspect it before close the connection if thehandleCloseFramesisfalse, default istrue. This implementation will establish the websocket connection once the connection to the remote server was complete. To know once a handshake was done you can intercept theChannelInboundHandler.userEventTriggered(ChannelHandlerContext, Object)and check if the event was of typeWebSocketClientProtocolHandler.ClientHandshakeStateEvent.HANDSHAKE_ISSUEDorWebSocketClientProtocolHandler.ClientHandshakeStateEvent.HANDSHAKE_COMPLETE.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classWebSocketClientProtocolHandler.ClientHandshakeStateEventEvents that are fired to notify about handshake status
-
Constructor Summary
Constructors Constructor Description WebSocketClientProtocolHandler(WebSocketClientHandshaker handshaker)Base constructorWebSocketClientProtocolHandler(WebSocketClientHandshaker handshaker, boolean handleCloseFrames)Base constructorWebSocketClientProtocolHandler(WebSocketClientHandshaker handshaker, boolean handleCloseFrames, boolean dropPongFrames)Base constructorWebSocketClientProtocolHandler(WebSocketClientHandshaker handshaker, boolean handleCloseFrames, boolean dropPongFrames, long handshakeTimeoutMillis)Base constructorWebSocketClientProtocolHandler(WebSocketClientHandshaker handshaker, boolean handleCloseFrames, boolean dropPongFrames, long handshakeTimeoutMillis, boolean withUTF8Validator)Base constructorWebSocketClientProtocolHandler(WebSocketClientHandshaker handshaker, boolean handleCloseFrames, long handshakeTimeoutMillis)Base constructorWebSocketClientProtocolHandler(WebSocketClientHandshaker handshaker, long handshakeTimeoutMillis)Base constructorWebSocketClientProtocolHandler(WebSocketClientHandshaker handshaker, WebSocketClientProtocolConfig clientConfig)Base constructorWebSocketClientProtocolHandler(WebSocketClientProtocolConfig clientConfig)Base constructorWebSocketClientProtocolHandler(URI webSocketURL, WebSocketVersion version, String subprotocol, boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength)Base constructorWebSocketClientProtocolHandler(URI webSocketURL, WebSocketVersion version, String subprotocol, boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength, boolean handleCloseFrames)Base constructorWebSocketClientProtocolHandler(URI webSocketURL, WebSocketVersion version, String subprotocol, boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength, boolean handleCloseFrames, boolean performMasking, boolean allowMaskMismatch)Base constructorWebSocketClientProtocolHandler(URI webSocketURL, WebSocketVersion version, String subprotocol, boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength, boolean handleCloseFrames, boolean performMasking, boolean allowMaskMismatch, long handshakeTimeoutMillis)Base constructorWebSocketClientProtocolHandler(URI webSocketURL, WebSocketVersion version, String subprotocol, boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength, boolean handleCloseFrames, long handshakeTimeoutMillis)Base constructorWebSocketClientProtocolHandler(URI webSocketURL, WebSocketVersion version, String subprotocol, boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength, long handshakeTimeoutMillis)Base constructor
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidbind(io.netty.channel.ChannelHandlerContext ctx, SocketAddress localAddress, io.netty.channel.ChannelPromise promise)protected WebSocketClientHandshakeExceptionbuildHandshakeException(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)WebSocketClientHandshakerhandshaker()Returns the used handshakervoidread(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, channelRead, channelReadComplete
-
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelActive, channelInactive, channelRegistered, channelUnregistered, channelWritabilityChanged, userEventTriggered
-
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, handlerRemoved, isSharable
-
-
-
-
Constructor Detail
-
WebSocketClientProtocolHandler
public WebSocketClientProtocolHandler(WebSocketClientProtocolConfig clientConfig)
Base constructor- Parameters:
clientConfig- Client protocol configuration.
-
WebSocketClientProtocolHandler
public WebSocketClientProtocolHandler(WebSocketClientHandshaker handshaker, WebSocketClientProtocolConfig clientConfig)
Base constructor- Parameters:
handshaker- TheWebSocketClientHandshakerwhich will be used to issue the handshake once the connection was established to the remote peer.clientConfig- Client protocol configuration.
-
WebSocketClientProtocolHandler
public WebSocketClientProtocolHandler(URI webSocketURL, WebSocketVersion version, String subprotocol, boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength, boolean handleCloseFrames, boolean performMasking, boolean allowMaskMismatch)
Base constructor- Parameters:
webSocketURL- URL for web socket communications. e.g "ws://myhost.com/mypath". Subsequent web socket frames will be sent to this URL.version- Version of web socket specification to use to connect to the serversubprotocol- Sub protocol request sent to the server.customHeaders- Map of custom headers to add to the client requestmaxFramePayloadLength- Maximum length of a frame's payloadhandleCloseFrames-trueif close frames should not be forwarded and just close the channelperformMasking- Whether to mask all written websocket frames. This must be set to true in order to be fully compatible with the websocket specifications. Client applications that communicate with a non-standard server which doesn't require masking might set this to false to achieve a higher performance.allowMaskMismatch- When set to true, frames which are not masked properly according to the standard will still be accepted.
-
WebSocketClientProtocolHandler
public WebSocketClientProtocolHandler(URI webSocketURL, WebSocketVersion version, String subprotocol, boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength, boolean handleCloseFrames, boolean performMasking, boolean allowMaskMismatch, long handshakeTimeoutMillis)
Base constructor- Parameters:
webSocketURL- URL for web socket communications. e.g "ws://myhost.com/mypath". Subsequent web socket frames will be sent to this URL.version- Version of web socket specification to use to connect to the serversubprotocol- Sub protocol request sent to the server.customHeaders- Map of custom headers to add to the client requestmaxFramePayloadLength- Maximum length of a frame's payloadhandleCloseFrames-trueif close frames should not be forwarded and just close the channelperformMasking- Whether to mask all written websocket frames. This must be set to true in order to be fully compatible with the websocket specifications. Client applications that communicate with a non-standard server which doesn't require masking might set this to false to achieve a higher performance.allowMaskMismatch- When set to true, frames which are not masked properly according to the standard will still be accepted.handshakeTimeoutMillis- Handshake timeout in mills, when handshake timeout, will trigger user eventWebSocketClientProtocolHandler.ClientHandshakeStateEvent.HANDSHAKE_TIMEOUT
-
WebSocketClientProtocolHandler
public WebSocketClientProtocolHandler(URI webSocketURL, WebSocketVersion version, String subprotocol, boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength, boolean handleCloseFrames)
Base constructor- Parameters:
webSocketURL- URL for web socket communications. e.g "ws://myhost.com/mypath". Subsequent web socket frames will be sent to this URL.version- Version of web socket specification to use to connect to the serversubprotocol- Sub protocol request sent to the server.customHeaders- Map of custom headers to add to the client requestmaxFramePayloadLength- Maximum length of a frame's payloadhandleCloseFrames-trueif close frames should not be forwarded and just close the channel
-
WebSocketClientProtocolHandler
public WebSocketClientProtocolHandler(URI webSocketURL, WebSocketVersion version, String subprotocol, boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength, boolean handleCloseFrames, long handshakeTimeoutMillis)
Base constructor- Parameters:
webSocketURL- URL for web socket communications. e.g "ws://myhost.com/mypath". Subsequent web socket frames will be sent to this URL.version- Version of web socket specification to use to connect to the serversubprotocol- Sub protocol request sent to the server.customHeaders- Map of custom headers to add to the client requestmaxFramePayloadLength- Maximum length of a frame's payloadhandleCloseFrames-trueif close frames should not be forwarded and just close the channelhandshakeTimeoutMillis- Handshake timeout in mills, when handshake timeout, will trigger user eventWebSocketClientProtocolHandler.ClientHandshakeStateEvent.HANDSHAKE_TIMEOUT
-
WebSocketClientProtocolHandler
public WebSocketClientProtocolHandler(URI webSocketURL, WebSocketVersion version, String subprotocol, boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength)
Base constructor- Parameters:
webSocketURL- URL for web socket communications. e.g "ws://myhost.com/mypath". Subsequent web socket frames will be sent to this URL.version- Version of web socket specification to use to connect to the serversubprotocol- Sub protocol request sent to the server.customHeaders- Map of custom headers to add to the client requestmaxFramePayloadLength- Maximum length of a frame's payload
-
WebSocketClientProtocolHandler
public WebSocketClientProtocolHandler(URI webSocketURL, WebSocketVersion version, String subprotocol, boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength, long handshakeTimeoutMillis)
Base constructor- Parameters:
webSocketURL- URL for web socket communications. e.g "ws://myhost.com/mypath". Subsequent web socket frames will be sent to this URL.version- Version of web socket specification to use to connect to the serversubprotocol- Sub protocol request sent to the server.customHeaders- Map of custom headers to add to the client requestmaxFramePayloadLength- Maximum length of a frame's payloadhandshakeTimeoutMillis- Handshake timeout in mills, when handshake timeout, will trigger user eventWebSocketClientProtocolHandler.ClientHandshakeStateEvent.HANDSHAKE_TIMEOUT
-
WebSocketClientProtocolHandler
public WebSocketClientProtocolHandler(WebSocketClientHandshaker handshaker, boolean handleCloseFrames)
Base constructor- Parameters:
handshaker- TheWebSocketClientHandshakerwhich will be used to issue the handshake once the connection was established to the remote peer.handleCloseFrames-trueif close frames should not be forwarded and just close the channel
-
WebSocketClientProtocolHandler
public WebSocketClientProtocolHandler(WebSocketClientHandshaker handshaker, boolean handleCloseFrames, long handshakeTimeoutMillis)
Base constructor- Parameters:
handshaker- TheWebSocketClientHandshakerwhich will be used to issue the handshake once the connection was established to the remote peer.handleCloseFrames-trueif close frames should not be forwarded and just close the channelhandshakeTimeoutMillis- Handshake timeout in mills, when handshake timeout, will trigger user eventWebSocketClientProtocolHandler.ClientHandshakeStateEvent.HANDSHAKE_TIMEOUT
-
WebSocketClientProtocolHandler
public WebSocketClientProtocolHandler(WebSocketClientHandshaker handshaker, boolean handleCloseFrames, boolean dropPongFrames)
Base constructor- Parameters:
handshaker- TheWebSocketClientHandshakerwhich will be used to issue the handshake once the connection was established to the remote peer.handleCloseFrames-trueif close frames should not be forwarded and just close the channeldropPongFrames-trueif pong frames should not be forwarded
-
WebSocketClientProtocolHandler
public WebSocketClientProtocolHandler(WebSocketClientHandshaker handshaker, boolean handleCloseFrames, boolean dropPongFrames, long handshakeTimeoutMillis)
Base constructor- Parameters:
handshaker- TheWebSocketClientHandshakerwhich will be used to issue the handshake once the connection was established to the remote peer.handleCloseFrames-trueif close frames should not be forwarded and just close the channeldropPongFrames-trueif pong frames should not be forwardedhandshakeTimeoutMillis- Handshake timeout in mills, when handshake timeout, will trigger user eventWebSocketClientProtocolHandler.ClientHandshakeStateEvent.HANDSHAKE_TIMEOUT
-
WebSocketClientProtocolHandler
public WebSocketClientProtocolHandler(WebSocketClientHandshaker handshaker, boolean handleCloseFrames, boolean dropPongFrames, long handshakeTimeoutMillis, boolean withUTF8Validator)
Base constructor- Parameters:
handshaker- TheWebSocketClientHandshakerwhich will be used to issue the handshake once the connection was established to the remote peer.handleCloseFrames-trueif close frames should not be forwarded and just close the channeldropPongFrames-trueif pong frames should not be forwardedhandshakeTimeoutMillis- Handshake timeout in mills, when handshake timeout, will trigger user eventWebSocketClientProtocolHandler.ClientHandshakeStateEvent.HANDSHAKE_TIMEOUTwithUTF8Validator-trueif UTF8 validation of text frames should be enabled
-
WebSocketClientProtocolHandler
public WebSocketClientProtocolHandler(WebSocketClientHandshaker handshaker)
Base constructor- Parameters:
handshaker- TheWebSocketClientHandshakerwhich will be used to issue the handshake once the connection was established to the remote peer.
-
WebSocketClientProtocolHandler
public WebSocketClientProtocolHandler(WebSocketClientHandshaker handshaker, long handshakeTimeoutMillis)
Base constructor- Parameters:
handshaker- TheWebSocketClientHandshakerwhich will be used to issue the handshake once the connection was established to the remote peer.handshakeTimeoutMillis- Handshake timeout in mills, when handshake timeout, will trigger user eventWebSocketClientProtocolHandler.ClientHandshakeStateEvent.HANDSHAKE_TIMEOUT
-
-
Method Detail
-
handshaker
public WebSocketClientHandshaker handshaker()
Returns the used handshaker
-
decode
protected void decode(io.netty.channel.ChannelHandlerContext ctx, WebSocketFrame frame, List<Object> out) throws Exception- Throws:
Exception
-
buildHandshakeException
protected WebSocketClientHandshakeException buildHandshakeException(String message)
Returns aWebSocketHandshakeExceptionthat depends on which client or server pipeline this handler belongs. Should be overridden in implementation otherwise a default exception is used.
-
handlerAdded
public void handlerAdded(io.netty.channel.ChannelHandlerContext ctx)
- Specified by:
handlerAddedin interfaceio.netty.channel.ChannelHandler- Overrides:
handlerAddedin classio.netty.channel.ChannelHandlerAdapter
-
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
public void read(io.netty.channel.ChannelHandlerContext ctx) throws Exception- Specified by:
readin interfaceio.netty.channel.ChannelOutboundHandler- Throws:
Exception
-
flush
public void flush(io.netty.channel.ChannelHandlerContext ctx) throws Exception- Specified by:
flushin interfaceio.netty.channel.ChannelOutboundHandler- Throws:
Exception
-
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- Overrides:
exceptionCaughtin classio.netty.channel.ChannelInboundHandlerAdapter- Throws:
Exception
-
-