Skip to content

WebSocketTransport

Manages the WebSocket connection to the Discord voice gateway: socket lifecycle, binary and JSON message framing, and heartbeating. Detects zombified connections (unacknowledged heartbeat) and notifies the owner via the onZombified callback.

WebSocketTransport.socketActive  :: boolean
WebSocketTransport.socketDisconnecting  :: boolean
WebSocketTransport.socketOpening  :: boolean
WebSocketTransport.openAttempt  :: number

Counts opens and forceClose calls; an open that finishes under an older count was abandoned and leaves socketOpening alone.

WebSocketTransport.isResuming  :: boolean
WebSocketTransport.socketInstance  :: any?
WebSocketTransport.socketThread  :: thread?
WebSocketTransport.heartbeatInterval  :: number?
WebSocketTransport.heartbeatThread  :: thread?
WebSocketTransport.heartbeatAcknowledged  :: boolean
WebSocketTransport.heartbeatClockTime  :: number?
WebSocketTransport.heartbeatPing  :: number?
WebSocketTransport.sequenceAcknowledgement  :: number
WebSocketTransport.onConnected  :: (() -> ())?
WebSocketTransport.onDisconnected  :: ((code: number?, closedBySelf: boolean?) -> ())?
WebSocketTransport.onMessage  :: ((payload: any) -> ())?
WebSocketTransport.onZombified  :: (() -> ())?
WebSocketTransport.logger  :: Logger
WebSocketTransport : sendAsync ( data )  -> ()

Sends a raw message over the voice WebSocket.

Parameters

data: string | buffer
WebSocketTransport : handleMessage ( message ,  isBinary )  -> ()

Decodes an incoming voice gateway message and dispatches it to onMessage. A binary frame is unwrapped from the voice binary framing (a 2-byte sequence number, then the opcode); a text frame is decoded as JSON. When the socket cannot say which kind of frame it was (isBinary is nil), a message whose first byte is not { is treated as binary.

Parameters

message: string
isBinary: boolean?
WebSocketTransport : heartbeatAsync ( requested )  -> ()

Sends a voice heartbeat. If the previous heartbeat was never acknowledged the connection is marked as zombified and onZombified is fired instead.

Parameters

requested: boolean?
WebSocketTransport : heartbeatIn ( milliseconds )  -> ()

Schedules the next heartbeat to fire after the given number of milliseconds, cancelling any pending heartbeat.

Parameters

milliseconds: number
WebSocketTransport : stopHeartbeat ( )  -> ()

Cancels the pending heartbeat, if any.

WebSocketTransport : openSocket ( url ,  isStillWanted )  -> ()

Opens the WebSocket to the given URL, starts the receive loop, and fires onConnected. Raises when a socket is already open or opening. When isStillWanted returns false once the handshake finishes, or forceClose abandoned the open during it, the new socket is closed at once and nothing starts.

Parameters

url: string
isStillWanted: (() -> boolean)?
WebSocketTransport : forceClose ( code )  -> ()

Synchronously closes the voice WebSocket and kills all associated threads. Safe to call from a signal handler or any context where yielding is not permitted. An open still in its handshake is abandoned: it closes its socket when the handshake finishes, and a new open may start at once.

Parameters

code: number?
WebSocketTransport . new ( loggerInstance )  -> WebSocketTransport

Responsible for constructing this class.

Parameters

loggerInstance: Logger

Returns

WebSocketTransport