Module io.netty5.codec.http
Class WebSocketClientProtocolHandler
- java.lang.Object
-
- io.netty5.channel.ChannelHandlerAdapter
-
- io.netty5.handler.codec.MessageToMessageDecoder<WebSocketFrame>
-
- io.netty5.handler.codec.http.websocketx.WebSocketClientProtocolHandler
-
- All Implemented Interfaces:
ChannelHandler
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 theChannelHandler.channelInboundEvent(ChannelHandlerContext, Object)and check if the event was of typeWebSocketHandshakeCompletionEvent.
-
-
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, long handshakeTimeoutMillis)Base constructorWebSocketClientProtocolHandler(WebSocketClientHandshaker handshaker, long handshakeTimeoutMillis)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 protected WebSocketClientHandshakeExceptionbuildHandshakeException(String message)Returns aWebSocketHandshakeExceptionthat depends on which client or server pipeline this handler belongs.voidchannelExceptionCaught(ChannelHandlerContext ctx, Throwable cause)Gets called if aThrowablewas thrown when handling inbound events.Future<Void>close(ChannelHandlerContext ctx)Called once a close operation is made.protected voiddecode(ChannelHandlerContext ctx, WebSocketFrame msg)Decode from one message to another.protected voiddecodeAndClose(ChannelHandlerContext ctx, WebSocketFrame frame)Decode from one message to another.voidhandlerAdded(ChannelHandlerContext ctx)Gets called after theChannelHandlerwas added to the actual context and it's ready to handle events.WebSocketClientHandshakerhandshaker()Returns the used handshakerFuture<Void>write(ChannelHandlerContext ctx, Object msg)Called once a write operation is made.-
Methods inherited from class io.netty5.handler.codec.MessageToMessageDecoder
acceptInboundMessage, channelRead
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.netty5.channel.ChannelHandler
bind, channelActive, channelInactive, channelInboundEvent, channelReadComplete, channelRegistered, channelShutdown, channelUnregistered, channelWritabilityChanged, connect, deregister, disconnect, flush, handlerRemoved, isSharable, pendingOutboundBytes, read, register, sendOutboundEvent, shutdown
-
-
-
-
Constructor Detail
-
WebSocketClientProtocolHandler
public WebSocketClientProtocolHandler(WebSocketClientProtocolConfig clientConfig)
Base constructor- Parameters:
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 an inbound channel eventWebSocketClientHandshakeCompletionEventwith aWebSocketHandshakeTimeoutException.
-
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 an inbound channel eventWebSocketClientHandshakeCompletionEventwith aWebSocketHandshakeTimeoutException.
-
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 an inbound channel eventWebSocketClientHandshakeCompletionEventwith aWebSocketHandshakeTimeoutException.
-
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 an inbound channel eventWebSocketClientHandshakeCompletionEventwith aWebSocketHandshakeTimeoutException.
-
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 an inbound channel eventWebSocketClientHandshakeCompletionEventwith aWebSocketHandshakeTimeoutException.
-
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 an inbound channel eventWebSocketClientHandshakeCompletionEventwith aWebSocketHandshakeTimeoutException.
-
-
Method Detail
-
handshaker
public WebSocketClientHandshaker handshaker()
Returns the used handshaker
-
decodeAndClose
protected void decodeAndClose(ChannelHandlerContext ctx, WebSocketFrame frame) throws Exception
Description copied from class:MessageToMessageDecoderDecode from one message to another. This method will be called for each written message that can be handled by this decoder.The message will not be automatically disposed of after this call. Instead, the responsibility of ensuring that messages are disposed of falls upon the implementor of this method.
Subclasses that wish to have incoming messages automatically disposed of should instead override the
MessageToMessageDecoder.decodeAndClose(ChannelHandlerContext, Object)method.- Parameters:
ctx- theChannelHandlerContextwhich thisMessageToMessageDecoderbelongs toframe- the message to decode to another one- Throws:
Exception- is thrown if an error occurs
-
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(ChannelHandlerContext ctx)
Description copied from interface:ChannelHandlerGets called after theChannelHandlerwas added to the actual context and it's ready to handle events.
-
decode
protected void decode(ChannelHandlerContext ctx, WebSocketFrame msg) throws Exception
Description copied from class:MessageToMessageDecoderDecode from one message to another. This method will be called for each written message that can be handled by this decoder.The message will be disposed of after this call.
Subclasses that wish to sometimes pass messages through, should instead override the
MessageToMessageDecoder.decodeAndClose(ChannelHandlerContext, Object)method.- Overrides:
decodein classMessageToMessageDecoder<WebSocketFrame>- Parameters:
ctx- theChannelHandlerContextwhich thisMessageToMessageDecoderbelongs tomsg- the message to decode to another one- Throws:
Exception- is thrown if an error occurs
-
close
public Future<Void> close(ChannelHandlerContext ctx)
Description copied from interface:ChannelHandlerCalled once a close operation is made.- Parameters:
ctx- theChannelHandlerContextfor which the close operation is made- Returns:
- the
Futurewhich will be notified once the operation completes.
-
write
public Future<Void> write(ChannelHandlerContext ctx, Object msg)
Description copied from interface:ChannelHandlerCalled 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.- Parameters:
ctx- theChannelHandlerContextfor which the write operation is mademsg- the message to write- Returns:
- the
Futurewhich will be notified once the operation completes.
-
channelExceptionCaught
public void channelExceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception
Description copied from interface:ChannelHandlerGets called if aThrowablewas thrown when handling inbound events.- Throws:
Exception
-
-