Class WebSocketClient

java.lang.Object
de.craftsblock.craftsnet.api.websocket.WebSocketClient
All Implemented Interfaces:
RequireAble, Runnable

public class WebSocketClient extends Object implements Runnable, RequireAble
The WebSocketClient class represents a WebSocket client that connects to the WebSocketServer.

When a WebSocket client connects, the server creates a new instance of this class to manage the client's connection. The client sends a handshake request, and if the server validates it, the client is assigned to a specific endpoint based on the requested path. The client continuously listens for incoming messages and invokes the appropriate endpoint's method to process the received data.

The class also handles the disconnection of the client, sending messages, and reading headers and messages from the socket.

Since:
2.1.1-SNAPSHOT
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    WebSocketClient(CraftsNet craftsNet, Socket socket, WebSocketServer server)
    Creates a new WebSocketClient with the provided socket and server.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Disconnects the client gracefully without providing any information about the reason.
    void
    close(@org.jetbrains.annotations.Range(from=1000L, to=4999L) int code, String reason)
    Disconnects the client gracefully with a specified code and reason.
    void
    close(ClosureCode code, String reason)
    Disconnects the client gracefully with a specified pre-defined code and reason.
    void
    close(String reason)
    Disconnects the client gracefully with a specified reason.
    protected void
    Disconnects the WebSocket client and performs necessary cleanup operations.
    Returns the domain used by the connected client.
    Retrieves the registered endpoint corresponding to the given header.
    int
    Returns the size which should every fragment of a frame should have.
    Retrieves the value of the given header key from the client's headers.
    Returns a List containing all the headers received from the client.
    Returns the IP address of the connected client.
    Returns the requested path from the client's headers.
    Returns the storage of this websocket client for storing specific data.
    boolean
    Returns whether the websocket runnable has been started.
    boolean
    Returns whether the web socket is still connected or the connection has been closed / failed.
    void
    run()
    Runs the WebSocket client to handle incoming messages and manage connections.
    void
    sendMessage(byte[] data)
    Sends a message to the connected WebSocket client.
    void
    sendMessage(de.craftsblock.craftscore.json.Json data)
    Sends a message to the connected WebSocket client.
    void
    Sends a message to the connected WebSocket client.
    void
    Sends a message to the connected WebSocket client.
    void
    Sends a message to the connected WebSocket client.
    void
    Sends a ping to the connected WebSocket client.
    void
    sendPing(byte[] message)
    Sends a ping with the provided payload to the connected WebSocket client.
    void
    sendPing(String message)
    Sends a ping with the provided payload to the connected WebSocket client.
    void
    Sends a pong to the connected WebSocket client.
    void
    sendPong(byte[] message)
    Sends a pong with the provided payload to the connected WebSocket client.
    void
    sendPong(String message)
    Sends a pong with the provided payload to the connected WebSocket client.
    void
    setFragmentationEnabled(boolean shouldFragment)
    Enable or disable fragmentation of messages send by the server.
    void
    setFragmentSize(int fragmentSize)
    Sets the maximum size of each fragment of a frame.
    void
    setMaskingOutgoing(boolean shouldMaskOutgoing)
    Sets whether the outgoing message should be masked or not.
    boolean
    Returns whether fragmentation is enabled or not.
    boolean
    Returns whether the outgoing messages should be masked or not.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • WebSocketClient

      public WebSocketClient(CraftsNet craftsNet, Socket socket, WebSocketServer server)
      Creates a new WebSocketClient with the provided socket and server.
      Parameters:
      craftsNet - The CraftsNet instance which instantiates this
      socket - The Socket used for communication with the client.
      server - The WebSocketServer to which this client belongs.
  • Method Details

    • run

      public void run()
      Runs the WebSocket client to handle incoming messages and manage connections. This method reads the client's headers, sends a handshake to establish the WebSocket connection, and then processes incoming messages from the client using a registered endpoint.
      Specified by:
      run in interface Runnable
    • getEndpoint

      @Nullable public @Nullable EnumMap<ProcessPriority.Priority,List<RouteRegistry.EndpointMapping>> getEndpoint()
      Retrieves the registered endpoint corresponding to the given header.
      Returns:
      The corresponding list of SocketMappings if found, or null if not found.
    • getPath

      public String getPath()
      Returns the requested path from the client's headers.
      Returns:
      The requested path as a String.
    • getHeader

      public String getHeader(String key)
      Retrieves the value of the given header key from the client's headers.
      Parameters:
      key - The header key for which the value is requested.
      Returns:
      The value of the header if found, or null if the header is not present.
    • getIp

      public String getIp()
      Returns the IP address of the connected client.
      Returns:
      The IP address as a String.
    • getDomain

      public String getDomain()
      Returns the domain used by the connected client.
      Returns:
      The domain as a String.
    • getHeaders

      public Headers getHeaders()
      Returns a List containing all the headers received from the client.
      Returns:
      A List of Strings representing the headers from the client.
    • getSession

      public Session getSession()
      Returns the storage of this websocket client for storing specific data.
      Returns:
      The storage of this websocket client instance.
    • shouldMaskOutgoing

      public boolean shouldMaskOutgoing()
      Returns whether the outgoing messages should be masked or not.
      Returns:
      true if the outgoing messages should be masked, false otherwise.
    • setMaskingOutgoing

      public void setMaskingOutgoing(boolean shouldMaskOutgoing)
      Sets whether the outgoing message should be masked or not.
      Parameters:
      shouldMaskOutgoing - true if the outgoing messages should be masked, false otherwise.
    • shouldFragment

      @Experimental public boolean shouldFragment()
      Returns whether fragmentation is enabled or not.
      Returns:
      true if fragmentation is enabled, false otherwise
    • setFragmentationEnabled

      @Experimental public void setFragmentationEnabled(boolean shouldFragment)
      Enable or disable fragmentation of messages send by the server.
      Parameters:
      shouldFragment - true if fragmentation should be enabled, false otherwise.
    • getFragmentSize

      @Experimental public int getFragmentSize()
      Returns the size which should every fragment of a frame should have.
      Returns:
      The max size of each frame.
    • setFragmentSize

      @Experimental public void setFragmentSize(int fragmentSize)
      Sets the maximum size of each fragment of a frame.
      Parameters:
      fragmentSize - The max size of the fragments.
    • sendMessage

      public void sendMessage(de.craftsblock.craftscore.json.Json data)
      Sends a message to the connected WebSocket client.
      Parameters:
      data - The message to be sent, as it's json representation.
    • sendMessage

      public void sendMessage(String data)
      Sends a message to the connected WebSocket client.
      Parameters:
      data - The message to be sent, as it's string representation.
    • sendMessage

      public void sendMessage(ByteBuffer data)
      Sends a message to the connected WebSocket client.
      Parameters:
      data - The message to be sent, as a bytebuffer.
    • sendMessage

      public void sendMessage(byte[] data)
      Sends a message to the connected WebSocket client.
      Parameters:
      data - The message to be sent, as an array of bytes.
    • sendMessage

      public void sendMessage(Object data)
      Sends a message to the connected WebSocket client.

      This method will try to parse the object in the following order:

      1. String
      2. byte[]
      3. Json
      4. ByteBuffer
      If none of these types can be applied, the object is converted into a string with Object.toString() and then sent.
      Parameters:
      data - The message to be sent, as an object.
      Since:
      3.4.3
    • sendPing

      public void sendPing()
      Sends a ping to the connected WebSocket client.
    • sendPing

      public void sendPing(String message)
      Sends a ping with the provided payload to the connected WebSocket client.
      Parameters:
      message - The payload as a string which should be appended
    • sendPing

      public void sendPing(byte[] message)
      Sends a ping with the provided payload to the connected WebSocket client.
      Parameters:
      message - The payload as a byte array which should be appended
    • sendPong

      public void sendPong()
      Sends a pong to the connected WebSocket client.
    • sendPong

      public void sendPong(String message)
      Sends a pong with the provided payload to the connected WebSocket client.
      Parameters:
      message - The payload as a string which should be appended
    • sendPong

      public void sendPong(byte[] message)
      Sends a pong with the provided payload to the connected WebSocket client.
      Parameters:
      message - The payload as a byte array which should be appended
    • close

      public void close()
      Disconnects the client gracefully without providing any information about the reason.
    • close

      public void close(String reason)
      Disconnects the client gracefully with a specified reason.
      Parameters:
      reason - The reason why the client has been closed.
      Throws:
      IllegalStateException - If the code used to close the connection is only for internal use.
    • close

      public void close(ClosureCode code, String reason)
      Disconnects the client gracefully with a specified pre-defined code and reason.
      Parameters:
      code - The pre-defined code that is responsible for closing the socket.
      reason - The reason why the client has been closed.
      Throws:
      IllegalStateException - If the code used to close the connection is only for internal use.
    • close

      public void close(@org.jetbrains.annotations.Range(from=1000L, to=4999L) int code, String reason)
      Disconnects the client gracefully with a specified code and reason.
      Parameters:
      code - The close code.
      reason - The reason why the client has been closed.
      Throws:
      IllegalStateException - If the code used to close the connection is only for internal use.
    • isActive

      public boolean isActive()
      Returns whether the websocket runnable has been started.
      Returns:
      True if the websocket runnable was started, false otherwise
    • isConnected

      public boolean isConnected()
      Returns whether the web socket is still connected or the connection has been closed / failed.
      Returns:
      True if the websocket is connected, false otherwise or if the connection failed
    • disconnect

      protected void disconnect()
      Disconnects the WebSocket client and performs necessary cleanup operations. This method triggers the ClientDisconnectEvent before closing the socket and removing the client from the server.