Class NettyWebSocketServerChannel

java.lang.Object
eu.cloudnetservice.driver.network.netty.http.NettyWebSocketServerChannel
All Implemented Interfaces:
WebSocketChannel, AutoCloseable

final class NettyWebSocketServerChannel extends Object implements WebSocketChannel
The default netty based implementation of a web socket channel.
Since:
4.0
  • Field Details

    • webSocketListeners

      private final Collection<WebSocketListener> webSocketListeners
    • channel

      private final io.netty5.channel.Channel channel
    • httpChannel

      private final HttpChannel 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

      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:
      addListener in interface WebSocketChannel
      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:
      removeListener in interface WebSocketChannel
      Parameters:
      listeners - the listeners to remove.
      Returns:
      the same instance as used to call the method, for chaining.
    • removeListener

      @NonNull public @NonNull WebSocketChannel removeListener(@NonNull @NonNull ClassLoader classLoader)
      Removes all listeners from this channel whose classes were loaded by the given class loader.
      Specified by:
      removeListener in interface WebSocketChannel
      Parameters:
      classLoader - the class loader to remove the listener of.
      Returns:
      the same instance as used to call the method, for chaining.
    • clearListeners

      @NonNull public @NonNull WebSocketChannel clearListeners()
      Removes all previously registered listeners from this channel.
      Specified by:
      clearListeners in interface WebSocketChannel
      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:
      listeners in interface WebSocketChannel
      Returns:
      all registered listeners on this channel.
    • sendWebSocketFrame

      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 to channel.sendWebSocketFrame(type, text.getBytes(StandardCharsets.UTF_8)).
      Specified by:
      sendWebSocketFrame in interface WebSocketChannel
      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 use WebSocketChannel.close(int, String) instead.
      Specified by:
      sendWebSocketFrame in interface WebSocketChannel
      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

      @NonNull public @NonNull HttpChannel channel()
      Get the underlying channel which was upgraded to this web socket channel.
      Specified by:
      channel in interface WebSocketChannel
      Returns:
      the original, now upgraded channel.
    • close

      public void close(int statusCode, @Nullable @Nullable String reasonText)
      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:
      close in interface WebSocketChannel
      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:
      close in interface AutoCloseable