The WebSocketClient class handles the creation and management of a WebSocket connection to a given server. It can send and receive messages, handle errors, and close the connection gracefully.

Constructors

Properties

Methods

Constructors

  • Creates an instance of WebSocketClient.

    Parameters

    • client: Client

      The main client instance that manages the bot or app's connection.

    • host: string

      The WebSocket server host URL (http or https will be converted to ws or wss).

    • token: string

      The authorization token used to authenticate with the WebSocket server.

    Returns WebSocketClient

Properties

_client: Client
conn: boolean
ws: WebSocket

Methods

  • Event handler for the "close" WebSocket event.

    This function is called when the WebSocket connection is closed. If the connection was established earlier, it sends a "Disconnected" message and logs the event. Otherwise, it logs a connection failure.

    Returns void

  • Event handler for the "error" WebSocket event.

    This function is called when an error occurs on the WebSocket connection. It logs the error message to the console.

    Parameters

    • err: ErrorEvent

      The error event containing details about the WebSocket error.

    Returns void

  • Helper function to check if a string is valid JSON.

    Parameters

    • data: string

      The string to check for valid JSON format.

    Returns boolean

    Returns true if the string is a valid JSON object or array, otherwise false.

  • Event handler for the "message" WebSocket event.

    This function is called when a message is received from the WebSocket server. If the message is valid JSON, it triggers the client event registration process. Otherwise, it logs the message.

    Parameters

    • d: any

      The raw message data received from the WebSocket server.

    Returns void

  • Event handler for the "open" WebSocket event.

    This function is called when the WebSocket connection is successfully established. It sends a "Connected" message to the server and logs the connection status.

    Returns void