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:
ChannelHandler,ChannelInboundHandler,ChannelOutboundHandler
public class WebSocketClientProtocolHandler extends 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-
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
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(java.net.URI webSocketURL, WebSocketVersion version, java.lang.String subprotocol, boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength)Base constructorWebSocketClientProtocolHandler(java.net.URI webSocketURL, WebSocketVersion version, java.lang.String subprotocol, boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength, boolean handleCloseFrames)Base constructorWebSocketClientProtocolHandler(java.net.URI webSocketURL, WebSocketVersion version, java.lang.String subprotocol, boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength, boolean handleCloseFrames, boolean performMasking, boolean allowMaskMismatch)Base constructorWebSocketClientProtocolHandler(java.net.URI webSocketURL, WebSocketVersion version, java.lang.String subprotocol, boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength, boolean handleCloseFrames, boolean performMasking, boolean allowMaskMismatch, long handshakeTimeoutMillis)Base constructorWebSocketClientProtocolHandler(java.net.URI webSocketURL, WebSocketVersion version, java.lang.String subprotocol, boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength, boolean handleCloseFrames, long handshakeTimeoutMillis)Base constructorWebSocketClientProtocolHandler(java.net.URI webSocketURL, WebSocketVersion version, java.lang.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(ChannelHandlerContext ctx, java.net.SocketAddress localAddress, ChannelPromise promise)Called once a bind operation is made.protected WebSocketClientHandshakeExceptionbuildHandshakeException(java.lang.String message)Returns aWebSocketHandshakeExceptionthat depends on which client or server pipeline this handler belongs.voidclose(ChannelHandlerContext ctx, ChannelPromise promise)Called once a close operation is made.voidconnect(ChannelHandlerContext ctx, java.net.SocketAddress remoteAddress, java.net.SocketAddress localAddress, ChannelPromise promise)Called once a connect operation is made.protected voiddecode(ChannelHandlerContext ctx, WebSocketFrame frame, java.util.List<java.lang.Object> out)Decode from one message to an other.voidderegister(ChannelHandlerContext ctx, ChannelPromise promise)Called once a deregister operation is made from the current registeredEventLoop.voiddisconnect(ChannelHandlerContext ctx, ChannelPromise promise)Called once a disconnect operation is made.voidexceptionCaught(ChannelHandlerContext ctx, java.lang.Throwable cause)CallsChannelHandlerContext.fireExceptionCaught(Throwable)to forward to the nextChannelHandlerin theChannelPipeline.voidflush(ChannelHandlerContext ctx)Called once a flush operation is made.voidhandlerAdded(ChannelHandlerContext ctx)Do nothing by default, sub-classes may override this method.WebSocketClientHandshakerhandshaker()Returns the used handshakervoidread(ChannelHandlerContext ctx)InterceptsChannelHandlerContext.read().voidwrite(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise)Called once a write operation is made.-
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
-
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
-
-
-
-
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(java.net.URI webSocketURL, WebSocketVersion version, java.lang.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(java.net.URI webSocketURL, WebSocketVersion version, java.lang.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(java.net.URI webSocketURL, WebSocketVersion version, java.lang.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(java.net.URI webSocketURL, WebSocketVersion version, java.lang.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(java.net.URI webSocketURL, WebSocketVersion version, java.lang.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(java.net.URI webSocketURL, WebSocketVersion version, java.lang.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(ChannelHandlerContext ctx, WebSocketFrame frame, java.util.List<java.lang.Object> out) throws java.lang.Exception
Description copied from class:MessageToMessageDecoderDecode from one message to an other. This method will be called for each written message that can be handled by this decoder.- Parameters:
ctx- theChannelHandlerContextwhich thisMessageToMessageDecoderbelongs toframe- the message to decode to an other oneout- theListto which decoded messages should be added- Throws:
java.lang.Exception- is thrown if an error occurs
-
buildHandshakeException
protected WebSocketClientHandshakeException buildHandshakeException(java.lang.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(ChannelHandlerContext ctx)
Description copied from class:ChannelHandlerAdapterDo nothing by default, sub-classes may override this method.- Specified by:
handlerAddedin interfaceChannelHandler- Overrides:
handlerAddedin classChannelHandlerAdapter
-
close
public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws java.lang.Exception
Description copied from interface:ChannelOutboundHandlerCalled once a close operation is made.- Specified by:
closein interfaceChannelOutboundHandler- Parameters:
ctx- theChannelHandlerContextfor which the close operation is madepromise- theChannelPromiseto notify once the operation completes- Throws:
java.lang.Exception- thrown if an error occurs
-
write
public void write(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise) throws java.lang.Exception
Description copied from interface:ChannelOutboundHandlerCalled once a write operation is made. The write operation will write the messages through theChannelPipeline. Those are then ready to be flushed to the actualChannelonceChannel.flush()is called- Specified by:
writein interfaceChannelOutboundHandler- Parameters:
ctx- theChannelHandlerContextfor which the write operation is mademsg- the message to writepromise- theChannelPromiseto notify once the operation completes- Throws:
java.lang.Exception- thrown if an error occurs
-
bind
public void bind(ChannelHandlerContext ctx, java.net.SocketAddress localAddress, ChannelPromise promise) throws java.lang.Exception
Description copied from interface:ChannelOutboundHandlerCalled once a bind operation is made.- Specified by:
bindin interfaceChannelOutboundHandler- Parameters:
ctx- theChannelHandlerContextfor which the bind operation is madelocalAddress- theSocketAddressto which it should boundpromise- theChannelPromiseto notify once the operation completes- Throws:
java.lang.Exception- thrown if an error occurs
-
connect
public void connect(ChannelHandlerContext ctx, java.net.SocketAddress remoteAddress, java.net.SocketAddress localAddress, ChannelPromise promise) throws java.lang.Exception
Description copied from interface:ChannelOutboundHandlerCalled once a connect operation is made.- Specified by:
connectin interfaceChannelOutboundHandler- Parameters:
ctx- theChannelHandlerContextfor which the connect operation is maderemoteAddress- theSocketAddressto which it should connectlocalAddress- theSocketAddresswhich is used as source on connectpromise- theChannelPromiseto notify once the operation completes- Throws:
java.lang.Exception- thrown if an error occurs
-
disconnect
public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws java.lang.Exception
Description copied from interface:ChannelOutboundHandlerCalled once a disconnect operation is made.- Specified by:
disconnectin interfaceChannelOutboundHandler- Parameters:
ctx- theChannelHandlerContextfor which the disconnect operation is madepromise- theChannelPromiseto notify once the operation completes- Throws:
java.lang.Exception- thrown if an error occurs
-
deregister
public void deregister(ChannelHandlerContext ctx, ChannelPromise promise) throws java.lang.Exception
Description copied from interface:ChannelOutboundHandlerCalled once a deregister operation is made from the current registeredEventLoop.- Specified by:
deregisterin interfaceChannelOutboundHandler- Parameters:
ctx- theChannelHandlerContextfor which the close operation is madepromise- theChannelPromiseto notify once the operation completes- Throws:
java.lang.Exception- thrown if an error occurs
-
read
public void read(ChannelHandlerContext ctx) throws java.lang.Exception
Description copied from interface:ChannelOutboundHandlerInterceptsChannelHandlerContext.read().- Specified by:
readin interfaceChannelOutboundHandler- Throws:
java.lang.Exception
-
flush
public void flush(ChannelHandlerContext ctx) throws java.lang.Exception
Description copied from interface:ChannelOutboundHandlerCalled once a flush operation is made. The flush operation will try to flush out all previous written messages that are pending.- Specified by:
flushin interfaceChannelOutboundHandler- Parameters:
ctx- theChannelHandlerContextfor which the flush operation is made- Throws:
java.lang.Exception- thrown if an error occurs
-
exceptionCaught
public void exceptionCaught(ChannelHandlerContext ctx, java.lang.Throwable cause) throws java.lang.Exception
Description copied from class:ChannelInboundHandlerAdapterCallsChannelHandlerContext.fireExceptionCaught(Throwable)to forward to the nextChannelHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
exceptionCaughtin interfaceChannelHandler- Specified by:
exceptionCaughtin interfaceChannelInboundHandler- Overrides:
exceptionCaughtin classChannelInboundHandlerAdapter- Throws:
java.lang.Exception
-
-