Class WebSocketClient

java.lang.Object
tech.underoaks.coldcase.remote.WebSocketClient

public class WebSocketClient extends Object
A WebSocket client for communicating with the remote game server.

This singleton class establishes and manages a WebSocket connection to the server, providing methods for sending and receiving messages.

  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Closes the current WebSocket session gracefully.
    void
    connect(String websocket_url)
    Connects to the WebSocket server using the specified WebSocket URL.
    void
    connect(String websocket_url, String session_id)
    Connects to the WebSocket server using the specified WebSocket URL and session ID.
    Retrieves the singleton instance of the WebSocket client.
    static String
    Retrieves the current lobby ID if it exists.
    boolean
    Checks if the WebSocket connection is open.
    void
    onClose(jakarta.websocket.Session session, jakarta.websocket.CloseReason closeReason)
    This method is invoked when the WebSocket connection is closed.
    void
    onMessage(String message)
    this message gets invoked when a message is received from the server.
    void
    onOpen(jakarta.websocket.Session session)
    This method is invoked when a WebSocket connection is established.
    void
    send(String message)
    Sends a message to the WebSocket server.

    Methods inherited from class java.lang.Object

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

    • WebSocketClient

      public WebSocketClient()
  • Method Details

    • connect

      public void connect(String websocket_url, String session_id)
      Connects to the WebSocket server using the specified WebSocket URL and session ID. If the session ID is "new", a new session will be created; otherwise, the provided session ID will be used.
      Parameters:
      websocket_url - the URL of the WebSocket server.
      session_id - the session ID to use (or "new" for a new session).
    • connect

      public void connect(String websocket_url)
      Connects to the WebSocket server using the specified WebSocket URL. The session ID is automatically set to "new" to create a new session.
      Parameters:
      websocket_url - the URL of the WebSocket server.
    • getInstance

      public static WebSocketClient getInstance()
      Retrieves the singleton instance of the WebSocket client.

      If the instance is not yet initialized, this method initializes it.

      Returns:
      the singleton instance of the WebSocket client.
    • getLobbyID

      public static String getLobbyID()
      Retrieves the current lobby ID if it exists. If the session ID is "new", it returns null.
      Returns:
      the current lobby ID, or null if a new session is created.
    • onOpen

      public void onOpen(jakarta.websocket.Session session)
      This method is invoked when a WebSocket connection is established. It triggers the corresponding action in the current game stage. !it doesn´t has access to the fields of this class
      Parameters:
      session - the WebSocket session.
    • onMessage

      public void onMessage(String message)
      this message gets invoked when a message is received from the server. it !it doesn´t has access to the fields of this class
      Parameters:
      message - the received message as a String.
    • onClose

      public void onClose(jakarta.websocket.Session session, jakarta.websocket.CloseReason closeReason)
      This method is invoked when the WebSocket connection is closed. It triggers the corresponding action in the current game stage. it !it doesn´t has access to the fields of this class
      Parameters:
      session - the WebSocket session.
      closeReason - the reason for the connection closure.
    • send

      public void send(String message)
      Sends a message to the WebSocket server.
      Parameters:
      message - the message to send as a String.
    • closeSession

      public boolean closeSession()
      Closes the current WebSocket session gracefully.

      This method checks if the WebSocket session is open and attempts to close it. If the session is already closed or not initialized, it simply returns true. In case of an IOException during the closing process, it logs the error and returns false.

      Returns:
      true if the session was closed successfully or was already closed, false if an error occurred while attempting to close the session.
    • isConnectionOpen

      public boolean isConnectionOpen()
      Checks if the WebSocket connection is open.
      Returns:
      true if the WebSocket session is open, false otherwise.