Package com.gmt2001.httpwsserver
Class WebSocketFrameHandler
java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelInboundHandlerAdapter
io.netty.channel.SimpleChannelInboundHandler<WebSocketFrame>
com.gmt2001.httpwsserver.WebSocketFrameHandler
- All Implemented Interfaces:
ChannelHandler
,ChannelInboundHandler
Processes WebSocket frames and passes successful ones to the appropriate registered final handler
- Author:
- gmt2001
-
Nested Class Summary
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final AttributeKey<String>
Represents theATTR_ALLOW_NON_SSL
attribute, which stores if the client is allowed to bypass SSL requirementsstatic final AttributeKey<WsFrameHandler>
Represents theATTR_FRAME_HANDLER
attribute, which stores theWsFrameHandler
that processes frames for this clientstatic final AttributeKey<String>
Represents theATTR_URI
attribute, which stores the URI the client has connected to -
Method Summary
Modifier and TypeMethodDescriptionstatic void
broadcastWsFrame
(WebSocketFrame resframe) Transmits aWebSocketFrame
to all authenticated clientsstatic void
broadcastWsFrame
(String uri, WebSocketFrame resframe) Transmits aWebSocketFrame
to all authenticated clients that are connected to a specific URIprotected void
channelRead0
(ChannelHandlerContext ctx, WebSocketFrame frame) Handles incoming WebSocket frames and passes them to the appropriateWsFrameHandler
static void
deregisterWsHandler
(String path) Deregisters a WS URI pathvoid
exceptionCaught
(ChannelHandlerContext ctx, Throwable cause) Handles exceptions that are thrown up the stackgetWsSessions
(String uri) static WebSocketFrame
prepareBinaryWebSocketResponse
(byte[] content) Creates and prepares a binary-typeWebSocketFrame
for transmissionstatic WebSocketFrame
prepareCloseWebSocketFrame
(int status, String reason) Creates and prepares a CloseWebSocketFrame
for transmissionstatic WebSocketFrame
Creates and prepares a CloseWebSocketFrame
for transmissionstatic WebSocketFrame
prepareTextWebSocketResponse
(String content) Creates and prepares a text-typeWebSocketFrame
for transmissionstatic WebSocketFrame
Creates and prepares a text-typeWebSocketFrame
for transmission from aJSONObject
static WebSocketFrame
Creates and prepares a text-typeWebSocketFrame
for transmission from aJSONStringer
static void
registerWsHandler
(String path, WsFrameHandler handler) Registers a WS URI path to aWsFrameHandler
static void
sendWsFrame
(ChannelHandlerContext ctx, WebSocketFrame reqframe, WebSocketFrame resframe) Transmits aWebSocketFrame
back to the clientstatic void
sendWsFrame
(Channel ch, WebSocketFrame reqframe, WebSocketFrame resframe) Transmits aWebSocketFrame
back to the clientvoid
userEventTriggered
(ChannelHandlerContext ctx, Object evt) CapturesWebSocketServerProtocolHandler.HandshakeComplete
events and saves theWsFrameHandler
URI to the session If a handler is not available for the requested path, then404 NOT FOUND
is sent back to the client using JSON:API formatMethods inherited from class io.netty.channel.SimpleChannelInboundHandler
acceptInboundMessage, channelRead
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelActive, channelInactive, channelReadComplete, channelRegistered, channelUnregistered, channelWritabilityChanged
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, handlerAdded, 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
handlerAdded, handlerRemoved
-
Field Details
-
ATTR_URI
Represents theATTR_URI
attribute, which stores the URI the client has connected to -
ATTR_FRAME_HANDLER
Represents theATTR_FRAME_HANDLER
attribute, which stores theWsFrameHandler
that processes frames for this client -
ATTR_ALLOW_NON_SSL
Represents theATTR_ALLOW_NON_SSL
attribute, which stores if the client is allowed to bypass SSL requirements
-
-
Method Details
-
channelRead0
Handles incoming WebSocket frames and passes them to the appropriateWsFrameHandler
- Specified by:
channelRead0
in classSimpleChannelInboundHandler<WebSocketFrame>
- Parameters:
ctx
- TheChannelHandlerContext
of the sessionframe
- TheWebSocketFrame
containing the request frame- Throws:
Exception
- Passes any thrown exceptions up the stack
-
userEventTriggered
CapturesWebSocketServerProtocolHandler.HandshakeComplete
events and saves theWsFrameHandler
URI to the session If a handler is not available for the requested path, then404 NOT FOUND
is sent back to the client using JSON:API format- Specified by:
userEventTriggered
in interfaceChannelInboundHandler
- Overrides:
userEventTriggered
in classChannelInboundHandlerAdapter
- Parameters:
ctx
- TheChannelHandlerContext
of the sessionevt
- The event object- Throws:
Exception
- Passes any thrown exceptions up the stack
-
exceptionCaught
Handles exceptions that are thrown up the stack- Specified by:
exceptionCaught
in interfaceChannelHandler
- Specified by:
exceptionCaught
in interfaceChannelInboundHandler
- Overrides:
exceptionCaught
in classChannelInboundHandlerAdapter
- Parameters:
ctx
- TheChannelHandlerContext
of the sessioncause
- The exception
-
prepareTextWebSocketResponse
Creates and prepares a text-typeWebSocketFrame
for transmission- Parameters:
content
- The content to send- Returns:
- A
WebSocketFrame
that is ready to transmit
-
prepareTextWebSocketResponse
Creates and prepares a text-typeWebSocketFrame
for transmission from aJSONObject
- Parameters:
json
- TheJSONObject
to send- Returns:
- A
WebSocketFrame
that is ready to transmit
-
prepareTextWebSocketResponse
Creates and prepares a text-typeWebSocketFrame
for transmission from aJSONStringer
- Parameters:
json
- TheJSONStringer
to send- Returns:
- A
WebSocketFrame
that is ready to transmit
-
prepareBinaryWebSocketResponse
Creates and prepares a binary-typeWebSocketFrame
for transmission- Parameters:
content
- The binary content to send- Returns:
- A
WebSocketFrame
that is ready to transmit
-
prepareCloseWebSocketFrame
Creates and prepares a CloseWebSocketFrame
for transmission- Parameters:
status
- TheWebSocketCloseStatus
to send- Returns:
- A
WebSocketFrame
that is ready to transmit
-
prepareCloseWebSocketFrame
Creates and prepares a CloseWebSocketFrame
for transmission- Parameters:
status
- The close status code to sendreason
- The reason string to send- Returns:
- A
WebSocketFrame
that is ready to transmit
-
sendWsFrame
public static void sendWsFrame(ChannelHandlerContext ctx, WebSocketFrame reqframe, WebSocketFrame resframe) Transmits aWebSocketFrame
back to the client- Parameters:
ctx
- TheChannelHandlerContext
of the sessionreqframe
- TheWebSocketFrame
containing the requestresframe
- TheWebSocketFrame
to transmit
-
sendWsFrame
Transmits aWebSocketFrame
back to the client- Parameters:
ch
- TheChannel
of the connectionreqframe
- TheWebSocketFrame
containing the requestresframe
- TheWebSocketFrame
to transmit
-
broadcastWsFrame
Transmits aWebSocketFrame
to all authenticated clients- Parameters:
resframe
- TheWebSocketFrame
to transmit
-
broadcastWsFrame
Transmits aWebSocketFrame
to all authenticated clients that are connected to a specific URI- Parameters:
uri
- The URI to filter clients by for the broadcastresframe
- TheWebSocketFrame
to transmit
-
getWsSessions
-
registerWsHandler
Registers a WS URI path to aWsFrameHandler
- Parameters:
path
- The URI path to bind the handler tohandler
- TheWsFrameHandler
that will handle the requests- Throws:
IllegalArgumentException
- Ifpath
is either already registered, or illegal- See Also:
-
validateUriPath
-
deregisterWsHandler
Deregisters a WS URI path- Parameters:
path
- The path to deregister
-