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_SSLattribute, which stores if the client is allowed to bypass SSL requirementsstatic final AttributeKey<WsFrameHandler>Represents theATTR_FRAME_HANDLERattribute, which stores theWsFrameHandlerthat processes frames for this clientstatic final AttributeKey<String>Represents theATTR_URIattribute, which stores the URI the client has connected to -
Method Summary
Modifier and TypeMethodDescriptionstatic voidbroadcastWsFrame(WebSocketFrame resframe) Transmits aWebSocketFrameto all authenticated clientsstatic voidbroadcastWsFrame(String uri, WebSocketFrame resframe) Transmits aWebSocketFrameto all authenticated clients that are connected to a specific URIprotected voidchannelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) Handles incoming WebSocket frames and passes them to the appropriateWsFrameHandlerstatic voidderegisterWsHandler(String path) Deregisters a WS URI pathvoidexceptionCaught(ChannelHandlerContext ctx, Throwable cause) Handles exceptions that are thrown up the stackgetWsSessions(String uri) static WebSocketFrameprepareBinaryWebSocketResponse(byte[] content) Creates and prepares a binary-typeWebSocketFramefor transmissionstatic WebSocketFrameprepareCloseWebSocketFrame(int status, String reason) Creates and prepares a CloseWebSocketFramefor transmissionstatic WebSocketFrameCreates and prepares a CloseWebSocketFramefor transmissionstatic WebSocketFrameprepareTextWebSocketResponse(String content) Creates and prepares a text-typeWebSocketFramefor transmissionstatic WebSocketFrameCreates and prepares a text-typeWebSocketFramefor transmission from aJSONObjectstatic WebSocketFrameCreates and prepares a text-typeWebSocketFramefor transmission from aJSONStringerstatic voidregisterWsHandler(String path, WsFrameHandler handler) Registers a WS URI path to aWsFrameHandlerstatic voidsendWsFrame(ChannelHandlerContext ctx, WebSocketFrame reqframe, WebSocketFrame resframe) Transmits aWebSocketFrameback to the clientstatic voidsendWsFrame(Channel ch, WebSocketFrame reqframe, WebSocketFrame resframe) Transmits aWebSocketFrameback to the clientvoiduserEventTriggered(ChannelHandlerContext ctx, Object evt) CapturesWebSocketServerProtocolHandler.HandshakeCompleteevents and saves theWsFrameHandlerURI to the session If a handler is not available for the requested path, then404 NOT FOUNDis sent back to the client using JSON:API formatMethods inherited from class io.netty.channel.SimpleChannelInboundHandler
acceptInboundMessage, channelReadMethods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelActive, channelInactive, channelReadComplete, channelRegistered, channelUnregistered, channelWritabilityChangedMethods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, handlerAdded, handlerRemoved, 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, handlerRemoved
-
Field Details
-
ATTR_URI
Represents theATTR_URIattribute, which stores the URI the client has connected to -
ATTR_FRAME_HANDLER
Represents theATTR_FRAME_HANDLERattribute, which stores theWsFrameHandlerthat processes frames for this client -
ATTR_ALLOW_NON_SSL
Represents theATTR_ALLOW_NON_SSLattribute, 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:
channelRead0in classSimpleChannelInboundHandler<WebSocketFrame>- Parameters:
ctx- TheChannelHandlerContextof the sessionframe- TheWebSocketFramecontaining the request frame- Throws:
Exception- Passes any thrown exceptions up the stack
-
userEventTriggered
CapturesWebSocketServerProtocolHandler.HandshakeCompleteevents and saves theWsFrameHandlerURI to the session If a handler is not available for the requested path, then404 NOT FOUNDis sent back to the client using JSON:API format- Specified by:
userEventTriggeredin interfaceChannelInboundHandler- Overrides:
userEventTriggeredin classChannelInboundHandlerAdapter- Parameters:
ctx- TheChannelHandlerContextof 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:
exceptionCaughtin interfaceChannelHandler- Specified by:
exceptionCaughtin interfaceChannelInboundHandler- Overrides:
exceptionCaughtin classChannelInboundHandlerAdapter- Parameters:
ctx- TheChannelHandlerContextof the sessioncause- The exception
-
prepareTextWebSocketResponse
Creates and prepares a text-typeWebSocketFramefor transmission- Parameters:
content- The content to send- Returns:
- A
WebSocketFramethat is ready to transmit
-
prepareTextWebSocketResponse
Creates and prepares a text-typeWebSocketFramefor transmission from aJSONObject- Parameters:
json- TheJSONObjectto send- Returns:
- A
WebSocketFramethat is ready to transmit
-
prepareTextWebSocketResponse
Creates and prepares a text-typeWebSocketFramefor transmission from aJSONStringer- Parameters:
json- TheJSONStringerto send- Returns:
- A
WebSocketFramethat is ready to transmit
-
prepareBinaryWebSocketResponse
Creates and prepares a binary-typeWebSocketFramefor transmission- Parameters:
content- The binary content to send- Returns:
- A
WebSocketFramethat is ready to transmit
-
prepareCloseWebSocketFrame
Creates and prepares a CloseWebSocketFramefor transmission- Parameters:
status- TheWebSocketCloseStatusto send- Returns:
- A
WebSocketFramethat is ready to transmit
-
prepareCloseWebSocketFrame
Creates and prepares a CloseWebSocketFramefor transmission- Parameters:
status- The close status code to sendreason- The reason string to send- Returns:
- A
WebSocketFramethat is ready to transmit
-
sendWsFrame
public static void sendWsFrame(ChannelHandlerContext ctx, WebSocketFrame reqframe, WebSocketFrame resframe) Transmits aWebSocketFrameback to the client- Parameters:
ctx- TheChannelHandlerContextof the sessionreqframe- TheWebSocketFramecontaining the requestresframe- TheWebSocketFrameto transmit
-
sendWsFrame
Transmits aWebSocketFrameback to the client- Parameters:
ch- TheChannelof the connectionreqframe- TheWebSocketFramecontaining the requestresframe- TheWebSocketFrameto transmit
-
broadcastWsFrame
Transmits aWebSocketFrameto all authenticated clients- Parameters:
resframe- TheWebSocketFrameto transmit
-
broadcastWsFrame
Transmits aWebSocketFrameto all authenticated clients that are connected to a specific URI- Parameters:
uri- The URI to filter clients by for the broadcastresframe- TheWebSocketFrameto transmit
-
getWsSessions
-
registerWsHandler
Registers a WS URI path to aWsFrameHandler- Parameters:
path- The URI path to bind the handler tohandler- TheWsFrameHandlerthat will handle the requests- Throws:
IllegalArgumentException- Ifpathis either already registered, or illegal- See Also:
-
validateUriPath
-
deregisterWsHandler
Deregisters a WS URI path- Parameters:
path- The path to deregister
-