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.
Summary
Properties
WebSocketTransport.socketActive :: booleanWebSocketTransport.socketDisconnecting :: booleanWebSocketTransport.socketOpening :: booleanWebSocketTransport.openAttempt :: numberWebSocketTransport.isResuming :: booleanWebSocketTransport.socketInstance :: any?WebSocketTransport.socketThread :: thread?WebSocketTransport.heartbeatInterval :: number?WebSocketTransport.heartbeatThread :: thread?WebSocketTransport.heartbeatAcknowledged :: booleanWebSocketTransport.heartbeatClockTime :: number?WebSocketTransport.heartbeatPing :: number?WebSocketTransport.sequenceAcknowledgement :: numberWebSocketTransport.onConnected :: (() -> ())?WebSocketTransport.onDisconnected :: ((code: number?, closedBySelf: boolean?) -> ())?WebSocketTransport.onMessage :: ((payload: any) -> ())?WebSocketTransport.onZombified :: (() -> ())?WebSocketTransport.logger :: logger.LoggerMethods
WebSocketTransport:sendAsync(data: string | buffer)WebSocketTransport:handleMessage(message: string, isBinary: boolean?)WebSocketTransport:heartbeatAsync(requested: boolean?)WebSocketTransport:heartbeatIn(milliseconds: number)WebSocketTransport:stopHeartbeat()WebSocketTransport:openSocket(url: string, isStillWanted: (() -> boolean)?)WebSocketTransport:forceClose(code: number?)Properties
Section titled “Properties”socketActive
Section titled “socketActive”socketDisconnecting
Section titled “socketDisconnecting”socketOpening
Section titled “socketOpening”openAttempt
Section titled “openAttempt”Counts opens and forceClose calls; an open that finishes under an older count was abandoned and leaves
socketOpening alone.
isResuming
Section titled “isResuming”socketInstance
Section titled “socketInstance”socketThread
Section titled “socketThread”heartbeatInterval
Section titled “heartbeatInterval”heartbeatThread
Section titled “heartbeatThread”heartbeatAcknowledged
Section titled “heartbeatAcknowledged”heartbeatClockTime
Section titled “heartbeatClockTime”heartbeatPing
Section titled “heartbeatPing”sequenceAcknowledgement
Section titled “sequenceAcknowledgement”onConnected
Section titled “onConnected”onDisconnected
Section titled “onDisconnected”onMessage
Section titled “onMessage”onZombified
Section titled “onZombified”logger
Section titled “logger”Methods
Section titled “Methods”sendAsync
Section titled “sendAsync”WebSocketTransport : sendAsync ( data ) -> ()
Sends a raw message over the voice WebSocket.
Parameters
| data: string | buffer |
handleMessage
Section titled “handleMessage”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.
heartbeatAsync
Section titled “heartbeatAsync”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? |
heartbeatIn
Section titled “heartbeatIn”WebSocketTransport : heartbeatIn ( milliseconds ) -> ()
Schedules the next heartbeat to fire after the given number of milliseconds, cancelling any pending heartbeat.
Parameters
| milliseconds: number |
stopHeartbeat
Section titled “stopHeartbeat”WebSocketTransport : stopHeartbeat ( ) -> ()
Cancels the pending heartbeat, if any.
openSocket
Section titled “openSocket”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)? |
forceClose
Section titled “forceClose”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? |
Functions
Section titled “Functions”WebSocketTransport . new ( loggerInstance ) -> WebSocketTransport
Responsible for constructing this class.
Parameters
| loggerInstance: Logger |
Returns
| WebSocketTransport |