Skip to content

WebSocketShard

classe · Source

Represents a Shard's WebSocket connection

Extends: EventEmitter

Properties

manager

manager: WebSocketManager

The WebSocketManager of the shard Source

id

id: number

The shard's id Source

resumeURL

nullable · private

resumeURL: string

The resume URL for this shard Source

status

status: Status

The current status of the shard Source

sequence

private

sequence: number

The current sequence of the shard Source

closeSequence

private

closeSequence: number

The sequence of the shard after close Source

sessionId

nullable · private

sessionId: string

The current session id of the shard Source

ping

ping: number

The previous heartbeat ping of the shard Source

lastPingTimestamp

private

lastPingTimestamp: number

The last time a ping was sent (a timestamp) Source

lastHeartbeatAcked

private

lastHeartbeatAcked: boolean

If we received a heartbeat ack back. Used to identify zombie connections Source

closeEmitted

private

closeEmitted: boolean

Used to prevent calling WebSocketShard#event:close twice while closing or terminating the WebSocket. Source

ratelimit

private

ratelimit: Object

Contains the rate limit queue and metadata Source

connection

nullable · private

connection: WebSocket

The WebSocket connection for the current shard Source

inflate

nullable · private

inflate: Inflate

The compression to use Source

helloTimeout

nullable · private

helloTimeout: NodeJSTimeout

The HELLO timeout Source

heartbeatTimeout

nullable · private

heartbeatTimeout: NodeJSTimeout

The timeout for the initial, jittered heartbeat. Source

heartbeatInterval

nullable · private

heartbeatInterval: NodeJSTimeout

The recurring heartbeat interval. Source

wsCloseTimeout

nullable · private

wsCloseTimeout: NodeJSTimeout

The WebSocket timeout. Source

eventsAttached

private

eventsAttached: boolean

If the manager attached its event handlers on the shard Source

expectedGuilds

nullable · private

expectedGuilds: Set<string>

A set of guild ids this shard expects to receive Source

readyTimeout

nullable · private

readyTimeout: NodeJSTimeout

The ready timeout Source

connectedAt

private

connectedAt: number

Time when the WebSocket connection was opened Source

Methods

debug

private

debug(message: string)

Emits a debug event. Parameters

NameTypeDescription
messagestringThe debug message
Source

connect

private

connect(): Promise<void>

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

private

onOpen()

Called whenever a connection is opened to the gateway. Source

onMessage

private

onMessage(event: MessageEvent)

Called whenever a message is received. Parameters

NameTypeDescription
eventMessageEventEvent received
Source

onError

private

onError(event: ErrorEvent)

Called whenever an error occurs with the WebSocket. Parameters

NameTypeDescription
eventErrorEventThe error that occurred
Source

onClose

private

onClose(event: CloseEvent)

Called whenever a connection to the gateway is closed. Parameters

NameTypeDescription
eventCloseEventClose event that was received
Source

emitClose

emitClose(event?: CloseEvent)

This method is responsible to emit close event for this shard. This method helps the shard reconnect. Parameters

NameTypeDescription
event?CloseEventClose event that was received
Source

onPacket

private

onPacket(packet: Object)

Called whenever a packet is received. Parameters

NameTypeDescription
packetObjectThe received packet
Source

checkReady

private

checkReady()

Checks if the shard can be marked as ready Source

setHelloTimeout

private

setHelloTimeout(time?: number)

Sets the HELLO packet timeout. Parameters

NameTypeDescription
time?numberIf set to -1, it will clear the hello timeout
Source

setWsCloseTimeout

private

setWsCloseTimeout(time?: number)

Sets the WebSocket Close timeout. This method is responsible for detecting any zombie connections if the WebSocket fails to close properly. Parameters

NameTypeDescription
time?numberIf set to -1, it will clear the timeout
Source

setHeartbeatTimer

private

setHeartbeatTimer(time: number)

Sets the heartbeat timer for this shard. Parameters

NameTypeDescription
timenumberIf -1, clears the timers; any other number schedules heartbeats
Source

sendHeartbeat

private

sendHeartbeat(tag?: string, ignoreHeartbeatAck?: boolean)

Sends a heartbeat to the WebSocket. If this shard didn't receive a heartbeat last time, it will destroy it and reconnect Parameters

NameTypeDescription
tag?stringWhat caused this heartbeat to be sent Default: 'HeartbeatTimer'.
ignoreHeartbeatAck?booleanIf we should send the heartbeat forcefully.
Source

ackHeartbeat

private

ackHeartbeat()

Acknowledges a heartbeat. Source

identify

private

identify(): void

Identifies the client on the connection. Returns: voidSource

identifyNew

private

identifyNew()

Identifies as a new connection on the gateway. Source

identifyResume

private

identifyResume()

Resumes a session on the gateway. Source

send

send(data: Object, important?: boolean)

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

NameTypeDescription
dataObjectThe full packet to send
important?booleanIf this packet should be added first in queue Default: false.
Source

_send

private

_send(data: Object): void

Sends data, bypassing the queue. Parameters

NameTypeDescription
dataObjectPacket to send

Returns: voidSource

processQueue

private

processQueue(): void

Processes the current WebSocket queue. Returns: voidSource

destroy

private

destroy(options?: Object)

Destroys this shard and closes its WebSocket connection. Parameters

NameTypeDescription
options?ObjectOptions for destroying the shard Default: { closeCode: 1000, reset: false, emit: true, log: true }.
Source

_cleanupConnection

private

_cleanupConnection()

Cleans up the WebSocket connection listeners. Source

_emitDestroyed

private

_emitDestroyed()

Emits the DESTROYED event on the shard Source

Events

close

close: unknown

Emitted when a shard's WebSocket closes. Parameters

NameTypeDescription
eventCloseEventThe received event
Source

ready

ready: unknown

Emitted when the shard receives the READY payload and is now waiting for guilds Source

resumed

resumed: unknown

Emitted when the shard resumes successfully Source

invalidSession

invalidSession: unknown

Emitted when the session has been invalidated. Source

allReady

allReady: unknown

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
NameTypeDescription
unavailableGuildsSet<string>Set of unavailable guilds, if any
Source

destroyed

destroyed: unknown

Emitted when a shard is destroyed, but no WebSocket connection was present. Source

Unofficial software. Not affiliated with or supported by Discord.