Skip to content

WebsocketManager

WebsocketManager ⇐ WebSocket

Represents a WebSocket manager that extends the WebSocket class.

Kind: global class
Extends: WebSocket

new WebsocketManager(client)

Constructs a new instance of the WebSocketClient class.

ParamTypeDescription
clientClientThe client object.

websocketManager.connect() ⇒

Establishes a connection to the WebSocket server. If the connection is not open, it will retry after a timeout. Retrieves the URL, shards, and session start limit from the bot gateway API. If the URL or session start limit is not available, or the remaining session start limit is less than 1, it logs an error message and exits the process. Sends an Identify opcode to the server with the client’s token, intents, presence, and properties. Logs the WebSocket information.

Kind: instance method of WebsocketManager
Returns: None

websocketManager.handleConnect() ⇒

Handles the connection of the websocket. If the websocket is closed, it logs a debug message and returns. Otherwise, it listens for incoming messages and creates a new instance of ActionsManager to handle the received data. It also listens for the “close” event and calls the handleClose method.

Kind: instance method of WebsocketManager
Returns: None

websocketManager.handleClose(err) ⇒ null

Handles the closing of the resource and returns the result of the error handling.

Kind: instance method of WebsocketManager
Returns: null - - Returns null if the error handling is successful.

ParamTypeDescription
errErrorThe error object to handle.

websocketManager.handleOpen() ⇒ void

Handles the “open” event of the WebSocket connection. If the connection is successfully opened, it logs a debug message and calls the handleConnect() function.

Kind: instance method of WebsocketManager

websocketManager.handleResume() ⇒ void

Handles the resumption of a WebSocket connection. If no session ID is found, it will re-identify and establish a new connection. If a session ID is found, it will attempt to resume the connection using the session ID.

Kind: instance method of WebsocketManager

websocketManager.handleReconnect() ⇒ void

Handles the reconnection process for the WebSocket connection. If there is no resume gateway URL, it will re-identify and connect again. If the status is not “CLOSED” and reconnect is enabled, it will initiate a reconnect. It will clear the heartbeat interval if it exists. Sets the status to “RECONNECTING”. Removes all event listeners. Sets a timeout to close the previous WebSocket connection and create a new one. If the previous connection is not closed, it will be forcefully closed. If the WebSocket is already closed, it will log a message. Creates a new WebSocket connection to the resume gateway URL. Sets the close sequence and marks the WebSocket as reconnected.

Kind: instance method of WebsocketManager

websocketManager.destroy(closeCode) ⇒ Promise

Destroys the current instance of the object.

Kind: instance method of WebsocketManager
Returns: Promise - - A promise that resolves when the instance is successfully destroyed.

ParamTypeDescription
closeCodecloseCodeThe code to use when closing the instance.

websocketManager.handleError(error) ⇒ void

Handles errors that occur during websocket communication.

Kind: instance method of WebsocketManager
Throws:

  • WebsocketError - Throws a WebsocketError with the corresponding error message and code.
ParamTypeDescription
errornumberThe error code.

websocketManager.send(payload) ⇒ Promise.<void>

Sends a payload over the websocket connection.

Kind: instance method of WebsocketManager
Returns: Promise.<void> - - A promise that resolves when the payload has been sent.

ParamTypeDescription
payloadanyThe payload to send.

WebsocketManager.transformPayload(payload) ⇒ object | null

Transforms a payload object by converting the “op” property to its corresponding opcode value.

Kind: static method of WebsocketManager
Returns: object | null - - The transformed payload object, or null if the “op” property is missing.

ParamTypeDescription
payloadobjectThe payload object to transform.