Package tech.underoaks.coldcase.remote
Class WebSocketClient
java.lang.Object
tech.underoaks.coldcase.remote.WebSocketClient
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 -
Method Summary
Modifier and TypeMethodDescriptionbooleanCloses the current WebSocket session gracefully.voidConnects to the WebSocket server using the specified WebSocket URL.voidConnects to the WebSocket server using the specified WebSocket URL and session ID.static WebSocketClientRetrieves the singleton instance of the WebSocket client.static StringRetrieves the current lobby ID if it exists.booleanChecks if the WebSocket connection is open.voidonClose(jakarta.websocket.Session session, jakarta.websocket.CloseReason closeReason) This method is invoked when the WebSocket connection is closed.voidthis message gets invoked when a message is received from the server.voidonOpen(jakarta.websocket.Session session) This method is invoked when a WebSocket connection is established.voidSends a message to the WebSocket server.
-
Constructor Details
-
WebSocketClient
public WebSocketClient()
-
-
Method Details
-
connect
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
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
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
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
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 aString.
-
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
Sends a message to the WebSocket server.- Parameters:
message- the message to send as aString.
-
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:
trueif the session was closed successfully or was already closed,falseif an error occurred while attempting to close the session.
-
isConnectionOpen
public boolean isConnectionOpen()Checks if the WebSocket connection is open.- Returns:
trueif the WebSocket session is open,falseotherwise.
-