Class WebSocketServer

java.lang.Object
de.craftsblock.craftsnet.api.Server
de.craftsblock.craftsnet.api.websocket.WebSocketServer

public class WebSocketServer extends Server
The WebSocketServer class represents a simple WebSocket server implementation. It allows WebSocket clients to connect, manages their connections, and enables sending messages to connected clients. The server can be configured to use SSL encryption by providing the necessary SSL key file. It uses a ServerSocket to listen for incoming connections on the specified port.
Since:
2.1.1-SNAPSHOT
See Also:
  • Constructor Details

    • WebSocketServer

      public WebSocketServer(CraftsNet craftsNet, int port, boolean ssl)
      Constructs a WebSocketServer instance with the specified port number.
      Parameters:
      craftsNet - The CraftsNet instance which instantiates this websocket server.
      port - The port number on which the server will listen for incoming connections.
      ssl - A boolean flag indicating whether SSL encryption should be used (true for HTTPS, false for HTTP).
    • WebSocketServer

      public WebSocketServer(CraftsNet craftsNet, int port, int backlog, boolean ssl)
      Constructs a WebSocketServer instance with the specified port number and backlog size.
      Parameters:
      craftsNet - The CraftsNet instance which instantiates this websocket server.
      port - The port number on which the server will listen for incoming connections.
      backlog - The size of the backlog for the server socket.
      ssl - A boolean flag indicating whether SSL encryption should be used.
  • Method Details

    • bind

      public void bind(int port, int backlog)
      Binds the server to the specified port and backlog.
      Overrides:
      bind in class Server
      Parameters:
      port - The port number to bind the server to.
      backlog - The maximum number of pending connections the server's socket may have in the queue.
    • start

      public void start()
      Starts the server.
      Overrides:
      start in class Server
    • stop

      public void stop()
      Stops the server.
      Overrides:
      stop in class Server
    • awakeOrWarn

      public void awakeOrWarn()
      Perform necessary actions when the server is awakened or warn if needed.
      Specified by:
      awakeOrWarn in class Server
    • sleepIfNotNeeded

      public void sleepIfNotNeeded()
      Puts the server to sleep if it is not needed.
      Specified by:
      sleepIfNotNeeded in class Server
    • isEnabled

      public boolean isEnabled()
      Checks if the server is enabled.
      Overrides:
      isEnabled in class Server
      Returns:
      true if the server is enabled, false otherwise.
    • isSSL

      public boolean isSSL()
      Checks if SSL is enabled for the server.
      Overrides:
      isSSL in class Server
      Returns:
      true if SSL is enabled, false otherwise.
    • shouldUseSSL

      public boolean shouldUseSSL()
      Checks if SSL should be enabled for the server.
      Returns:
      true if SSL should be enabled, false otherwise.
    • getTypeEncoderRegistry

      public TypeEncoderRegistry<WebSocketSafeTypeEncoder<?,?>> getTypeEncoderRegistry()
      Retrieves the TypeEncoderRegistry instance managing the registration and lookup of WebSocketSafeTypeEncoder codecs.
      Returns:
      the TypeEncoderRegistry for WebSocketSafeTypeEncoder instances
      Since:
      3.5.0
    • 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.
    • broadcast

      public void broadcast(String data)
      Sends a message to all connected WebSocket clients.
      Parameters:
      data - The message to be sent.
    • broadcast

      public void broadcast(String path, String data)
      Sends a message to all connected WebSocket clients with a specified path.
      Parameters:
      path - The path to which the clients are assigned.
      data - The message to be sent.
    • add

      protected void add(String path, WebSocketClient client)
      Adds a WebSocket client to a specified path.
      Parameters:
      path - The path to which the client will be assigned.
      client - The WebSocket client that will be added.
    • remove

      protected void remove(WebSocketClient client)
      Removes a WebSocket client from the server and the associated path.
      Parameters:
      client - The WebSocket client that will be removed.
    • getClients

      public List<WebSocketClient> getClients()
      Retrieves the list of all currently connected WebSocketClients.
      Returns:
      The list of the connected WebSocketClients.