Class NettyWebSocketServerChannel
java.lang.Object
eu.cloudnetservice.driver.network.netty.http.NettyWebSocketServerChannel
- All Implemented Interfaces:
WebSocketChannel,AutoCloseable
The default netty based implementation of a web socket channel.
- Since:
- 4.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final io.netty5.channel.Channelprivate final HttpChannelprivate final Collection<WebSocketListener> -
Constructor Summary
ConstructorsConstructorDescriptionNettyWebSocketServerChannel(@NonNull HttpChannel httpChannel, @NonNull io.netty5.channel.Channel channel) Constructs a new netty web socket channel instance. -
Method Summary
Modifier and TypeMethodDescriptionaddListener(@NonNull WebSocketListener... listeners) Adds the given listeners to this web socket channel.channel()Get the underlying channel which was upgraded to this web socket channel.Removes all previously registered listeners from this channel.voidclose()voidSends a close frame into this channel and closes the connection to the recipient without waiting for any kind of response.Get all listeners which were registered to this channel previously.removeListener(@NonNull WebSocketListener... listeners) Removes the given listeners from the channel if they were added previously.removeListener(@NonNull ClassLoader classLoader) Removes all listeners from this channel whose classes were loaded by the given class loader.sendWebSocketFrame(@NonNull WebSocketFrameType webSocketFrameType, byte[] bytes) Sends a web socket frame of the given type into this channel.sendWebSocketFrame(@NonNull WebSocketFrameType type, @NonNull String text) Sends a web socket frame of the given type into this channel.
-
Field Details
-
webSocketListeners
-
channel
private final io.netty5.channel.Channel channel -
httpChannel
-
-
Constructor Details
-
NettyWebSocketServerChannel
public NettyWebSocketServerChannel(@NonNull @NonNull HttpChannel httpChannel, @NonNull @NonNull io.netty5.channel.Channel channel) Constructs a new netty web socket channel instance.- Parameters:
httpChannel- the original channel the upgrade request came from.channel- the unwrapped netty channel.- Throws:
NullPointerException- if either the given http or netty channel is null.
-
-
Method Details
-
addListener
@NonNull public @NonNull WebSocketChannel addListener(@NonNull @NonNull WebSocketListener... listeners) Adds the given listeners to this web socket channel. Each listener will be called when a web socket frame is received from the wrapped channel.- Specified by:
addListenerin interfaceWebSocketChannel- Parameters:
listeners- the listeners to add.- Returns:
- the same instance as used to call the method, for chaining.
-
removeListener
@NonNull public @NonNull WebSocketChannel removeListener(@NonNull @NonNull WebSocketListener... listeners) Removes the given listeners from the channel if they were added previously.- Specified by:
removeListenerin interfaceWebSocketChannel- Parameters:
listeners- the listeners to remove.- Returns:
- the same instance as used to call the method, for chaining.
-
removeListener
Removes all listeners from this channel whose classes were loaded by the given class loader.- Specified by:
removeListenerin interfaceWebSocketChannel- Parameters:
classLoader- the class loader to remove the listener of.- Returns:
- the same instance as used to call the method, for chaining.
-
clearListeners
Removes all previously registered listeners from this channel.- Specified by:
clearListenersin interfaceWebSocketChannel- Returns:
- the same instance as used to call the method, for chaining.
-
listeners
Get all listeners which were registered to this channel previously.- Specified by:
listenersin interfaceWebSocketChannel- Returns:
- all registered listeners on this channel.
-
sendWebSocketFrame
@NonNull public @NonNull WebSocketChannel sendWebSocketFrame(@NonNull @NonNull WebSocketFrameType type, @NonNull @NonNull String text) Sends a web socket frame of the given type into this channel. This method sends frames of the type PING, PONG and TEXT. Any other type will be silently converted to a binary frame holding the given text in its body. This method call is equivalent tochannel.sendWebSocketFrame(type, text.getBytes(StandardCharsets.UTF_8)).- Specified by:
sendWebSocketFramein interfaceWebSocketChannel- Parameters:
type- the type of web socket frame to send.text- the string content of the frame.- Returns:
- the same instance as used to call the method, for chaining.
-
sendWebSocketFrame
@NonNull public @NonNull WebSocketChannel sendWebSocketFrame(@NonNull @NonNull WebSocketFrameType webSocketFrameType, byte[] bytes) Sends a web socket frame of the given type into this channel. This method sends frames of the type PING, PONG and TEXT. Any other type will be silently converted to a binary frame holding the given bytes in its body. To send a close frame useWebSocketChannel.close(int, String)instead.- Specified by:
sendWebSocketFramein interfaceWebSocketChannel- Parameters:
webSocketFrameType- the type of web socket frame to send.bytes- the bytes to put into the frame body.- Returns:
- the same instance as used to call the method, for chaining.
-
channel
Get the underlying channel which was upgraded to this web socket channel.- Specified by:
channelin interfaceWebSocketChannel- Returns:
- the original, now upgraded channel.
-
close
Sends a close frame into this channel and closes the connection to the recipient without waiting for any kind of response. Any listener added to this channel can change the given reason status code and the reason text. The given status code must be valid as defined in RFC 6455.- Specified by:
closein interfaceWebSocketChannel- Parameters:
statusCode- the status code of the close, must be valid as per RFC-6455.reasonText- the reason text for the close, no text indicates no special reason.
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-