Class AbstractNettyWebSocketHandler
java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelInboundHandlerAdapter
io.netty.channel.SimpleChannelInboundHandler<Object>
io.micronaut.http.netty.websocket.AbstractNettyWebSocketHandler
- All Implemented Interfaces:
io.netty.channel.ChannelHandler,io.netty.channel.ChannelInboundHandler
- Direct Known Subclasses:
NettyServerWebSocketHandler,NettyWebSocketClientHandler
@Internal
public abstract class AbstractNettyWebSocketHandler
extends io.netty.channel.SimpleChannelInboundHandler<Object>
Abstract implementation that handles WebSocket frames.
- Since:
- 1.0
- Author:
- graemerocher
-
Nested Class Summary
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
io.netty.channel.ChannelHandler.Sharable -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final ConversionServicestatic final StringThe id of the handler used when adding it to the Netty pipeline.protected final org.slf4j.Loggerprotected final MediaTypeCodecRegistryprotected final MessageBodyHandlerRegistryprotected final MethodExecutionHandle<?,?> protected final HttpRequest<?>protected final MethodExecutionHandle<?,?> protected final Stringprotected final WebSocketBean<?>protected final ArgumentBinderRegistry<WebSocketState>protected final WebSocketSessionRepositoryprotected final io.netty.handler.codec.http.websocketx.WebSocketVersion -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractNettyWebSocketHandler(RequestBinderRegistry binderRegistry, MediaTypeCodecRegistry mediaTypeCodecRegistry, MessageBodyHandlerRegistry messageBodyHandlerRegistry, WebSocketBean<?> webSocketBean, HttpRequest<?> request, Map<String, Object> uriVariables, io.netty.handler.codec.http.websocketx.WebSocketVersion version, String subProtocol, WebSocketSessionRepository webSocketSessionRepository, ConversionService conversionService) Default constructor. -
Method Summary
Modifier and TypeMethodDescriptionprotected Publisher<?>callOpenMethod(io.netty.channel.ChannelHandlerContext ctx) Calls the open method of the websocket bean.protected voidchannelRead0(io.netty.channel.ChannelHandlerContext ctx, Object msg) protected abstract NettyWebSocketSessioncreateWebSocketSession(io.netty.channel.ChannelHandlerContext ctx) Subclasses should implement to create the actualNettyWebSocketSession.voidexceptionCaught(io.netty.channel.ChannelHandlerContext ctx, Throwable cause) protected final voidforwardErrorToUser(io.netty.channel.ChannelHandlerContext ctx, Consumer<Throwable> fallback, Throwable cause) abstract Argument<?>abstract Argument<?>abstract NettyWebSocketSessionprotected voidhandleCloseReason(io.netty.channel.ChannelHandlerContext ctx, CloseReason cr, boolean writeCloseReason) Used to close the session with a given reason.voidhandlerRemoved(io.netty.channel.ChannelHandlerContext ctx) protected voidhandleWebSocketFrame(io.netty.channel.ChannelHandlerContext ctx, io.netty.handler.codec.http.websocketx.WebSocketFrame msg) Handles WebSocket frame request.protected Publisher<?>instrumentPublisher(io.netty.channel.ChannelHandlerContext ctx, Object result) Subclasses can override to customize publishers returned from message handlers.protected ObjectinvokeExecutable(BoundExecutable boundExecutable, MethodExecutionHandle<?, ?> messageHandler) Invokes the given executable.protected voidmessageHandled(io.netty.channel.ChannelHandlerContext ctx, Object message) Method called once a message has been handled by the handler.protected voidwriteCloseFrameAndTerminate(io.netty.channel.ChannelHandlerContext ctx, CloseReason closeReason) Writes the give close reason and terminates the session.Methods inherited from class io.netty.channel.SimpleChannelInboundHandler
acceptInboundMessage, channelReadMethods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelActive, channelInactive, channelReadComplete, channelRegistered, channelUnregistered, channelWritabilityChanged, userEventTriggeredMethods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, handlerAdded, 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
handlerAdded
-
Field Details
-
ID
The id of the handler used when adding it to the Netty pipeline.- See Also:
-
LOG
protected final org.slf4j.Logger LOG -
webSocketBinder
-
uriVariables
-
webSocketBean
-
originatingRequest
-
messageHandler
-
pongHandler
-
mediaTypeCodecRegistry
-
messageBodyHandlerRegistry
-
webSocketVersion
protected final io.netty.handler.codec.http.websocketx.WebSocketVersion webSocketVersion -
subProtocol
-
webSocketSessionRepository
-
conversionService
-
-
Constructor Details
-
AbstractNettyWebSocketHandler
protected AbstractNettyWebSocketHandler(RequestBinderRegistry binderRegistry, MediaTypeCodecRegistry mediaTypeCodecRegistry, MessageBodyHandlerRegistry messageBodyHandlerRegistry, WebSocketBean<?> webSocketBean, HttpRequest<?> request, Map<String, Object> uriVariables, io.netty.handler.codec.http.websocketx.WebSocketVersion version, String subProtocol, WebSocketSessionRepository webSocketSessionRepository, ConversionService conversionService) Default constructor.- Parameters:
binderRegistry- The request binder registrymediaTypeCodecRegistry- The codec registrymessageBodyHandlerRegistry- The handler registrywebSocketBean- The websocket beanrequest- The originating requesturiVariables- The URI variablesversion- The websocket version being usedsubProtocol- The handler sub-protocolwebSocketSessionRepository- The web socket repository if they are supported (like on the server), null otherwiseconversionService- The conversion service
-
-
Method Details
-
callOpenMethod
Calls the open method of the websocket bean.- Parameters:
ctx- The handler context- Returns:
- Publisher for any errors, or the result of the open method
-
getBodyArgument
- Returns:
- The body argument for the message handler
-
getPongArgument
- Returns:
- The pong argument for the pong handler
-
getSession
- Returns:
- The session
-
exceptionCaught
- Specified by:
exceptionCaughtin interfaceio.netty.channel.ChannelHandler- Specified by:
exceptionCaughtin interfaceio.netty.channel.ChannelInboundHandler- Overrides:
exceptionCaughtin classio.netty.channel.ChannelInboundHandlerAdapter
-
forwardErrorToUser
-
handlerRemoved
- Specified by:
handlerRemovedin interfaceio.netty.channel.ChannelHandler- Overrides:
handlerRemovedin classio.netty.channel.ChannelHandlerAdapter- Throws:
Exception
-
createWebSocketSession
protected abstract NettyWebSocketSession createWebSocketSession(io.netty.channel.ChannelHandlerContext ctx) Subclasses should implement to create the actualNettyWebSocketSession.- Parameters:
ctx- The context- Returns:
- The session
-
instrumentPublisher
protected Publisher<?> instrumentPublisher(io.netty.channel.ChannelHandlerContext ctx, Object result) Subclasses can override to customize publishers returned from message handlers.- Parameters:
ctx- The contextresult- The result- Returns:
- The flowable
-
invokeExecutable
protected Object invokeExecutable(BoundExecutable boundExecutable, MethodExecutionHandle<?, ?> messageHandler) Invokes the given executable.- Parameters:
boundExecutable- The bound executablemessageHandler- The message handler- Returns:
- The result
-
channelRead0
- Specified by:
channelRead0in classio.netty.channel.SimpleChannelInboundHandler<Object>
-
handleWebSocketFrame
protected void handleWebSocketFrame(io.netty.channel.ChannelHandlerContext ctx, io.netty.handler.codec.http.websocketx.WebSocketFrame msg) Handles WebSocket frame request.- Parameters:
ctx- The contextmsg- The frame
-
messageHandled
Method called once a message has been handled by the handler.- Parameters:
ctx- The channel handler contextmessage- The message that was handled
-
writeCloseFrameAndTerminate
protected void writeCloseFrameAndTerminate(io.netty.channel.ChannelHandlerContext ctx, CloseReason closeReason) Writes the give close reason and terminates the session.- Parameters:
ctx- The contextcloseReason- The reason
-
handleCloseReason
protected void handleCloseReason(io.netty.channel.ChannelHandlerContext ctx, CloseReason cr, boolean writeCloseReason) Used to close the session with a given reason.- Parameters:
ctx- The contextcr- The reasonwriteCloseReason- Whether to allow writing the close reason to the remote
-