WebSocketShard
classe · Source
Represents a Shard's WebSocket connection
Extends: EventEmitter
Properties
manager
The WebSocketManager of the shard Source
id
The shard's id Source
resumeURL
The resume URL for this shard Source
status
The current status of the shard Source
sequence
The current sequence of the shard Source
closeSequence
The sequence of the shard after close Source
sessionId
The current session id of the shard Source
ping
The previous heartbeat ping of the shard Source
lastPingTimestamp
The last time a ping was sent (a timestamp) Source
lastHeartbeatAcked
If we received a heartbeat ack back. Used to identify zombie connections Source
closeEmitted
Used to prevent calling WebSocketShard#event:close twice while closing or terminating the WebSocket. Source
ratelimit
Contains the rate limit queue and metadata Source
connection
The WebSocket connection for the current shard Source
inflate
The compression to use Source
helloTimeout
The HELLO timeout Source
heartbeatTimeout
The timeout for the initial, jittered heartbeat. Source
heartbeatInterval
The recurring heartbeat interval. Source
wsCloseTimeout
The WebSocket timeout. Source
eventsAttached
If the manager attached its event handlers on the shard Source
expectedGuilds
A set of guild ids this shard expects to receive Source
readyTimeout
The ready timeout Source
connectedAt
Time when the WebSocket connection was opened Source
Methods
debug
Emits a debug event. Parameters
| Name | Type | Description |
|---|---|---|
message | string | The debug message |
| Source |
connect
Connects the shard to the gateway. Returns: Promise<void> — A promise that will resolve if the shard turns ready successfully, or reject if we couldn't connect Source
onOpen
Called whenever a connection is opened to the gateway. Source
onMessage
Called whenever a message is received. Parameters
| Name | Type | Description |
|---|---|---|
event | MessageEvent | Event received |
| Source |
onError
Called whenever an error occurs with the WebSocket. Parameters
| Name | Type | Description |
|---|---|---|
event | ErrorEvent | The error that occurred |
| Source |
onClose
Called whenever a connection to the gateway is closed. Parameters
| Name | Type | Description |
|---|---|---|
event | CloseEvent | Close event that was received |
| Source |
emitClose
This method is responsible to emit close event for this shard. This method helps the shard reconnect. Parameters
| Name | Type | Description |
|---|---|---|
event? | CloseEvent | Close event that was received |
| Source |
onPacket
Called whenever a packet is received. Parameters
| Name | Type | Description |
|---|---|---|
packet | Object | The received packet |
| Source |
checkReady
Checks if the shard can be marked as ready Source
setHelloTimeout
Sets the HELLO packet timeout. Parameters
| Name | Type | Description |
|---|---|---|
time? | number | If set to -1, it will clear the hello timeout |
| Source |
setWsCloseTimeout
Sets the WebSocket Close timeout. This method is responsible for detecting any zombie connections if the WebSocket fails to close properly. Parameters
| Name | Type | Description |
|---|---|---|
time? | number | If set to -1, it will clear the timeout |
| Source |
setHeartbeatTimer
Sets the heartbeat timer for this shard. Parameters
| Name | Type | Description |
|---|---|---|
time | number | If -1, clears the timers; any other number schedules heartbeats |
| Source |
sendHeartbeat
Sends a heartbeat to the WebSocket. If this shard didn't receive a heartbeat last time, it will destroy it and reconnect Parameters
| Name | Type | Description |
|---|---|---|
tag? | string | What caused this heartbeat to be sent Default: 'HeartbeatTimer'. |
ignoreHeartbeatAck? | boolean | If we should send the heartbeat forcefully. |
| Source |
ackHeartbeat
Acknowledges a heartbeat. Source
identify
Identifies the client on the connection. Returns: voidSource
identifyNew
Identifies as a new connection on the gateway. Source
identifyResume
Resumes a session on the gateway. Source
send
Adds a packet to the queue to be sent to the gateway. If you use this method, make sure you understand that you need to provide a full Payload. Do not use this method if you don't know what you're doing. Parameters
| Name | Type | Description |
|---|---|---|
data | Object | The full packet to send |
important? | boolean | If this packet should be added first in queue Default: false. |
| Source |
_send
Sends data, bypassing the queue. Parameters
| Name | Type | Description |
|---|---|---|
data | Object | Packet to send |
Returns: voidSource
processQueue
Processes the current WebSocket queue. Returns: voidSource
destroy
Destroys this shard and closes its WebSocket connection. Parameters
| Name | Type | Description |
|---|---|---|
options? | Object | Options for destroying the shard Default: { closeCode: 1000, reset: false, emit: true, log: true }. |
| Source |
_cleanupConnection
Cleans up the WebSocket connection listeners. Source
_emitDestroyed
Emits the DESTROYED event on the shard Source
Events
close
Emitted when a shard's WebSocket closes. Parameters
| Name | Type | Description |
|---|---|---|
event | CloseEvent | The received event |
| Source |
ready
Emitted when the shard receives the READY payload and is now waiting for guilds Source
resumed
Emitted when the shard resumes successfully Source
invalidSession
Emitted when the session has been invalidated. Source
allReady
Emitted when the shard is fully ready. This event is emitted if:
- all guilds were received by this shard
- the ready timeout expired, and some guilds are unavailable Parameters
| Name | Type | Description |
|---|---|---|
unavailableGuilds | Set<string> | Set of unavailable guilds, if any |
| Source |
destroyed
Emitted when a shard is destroyed, but no WebSocket connection was present. Source