detritus

Socketclass

A connection to a voice server's gateway, created by Gateway.Socket.voiceConnect().

On this page
import { Media } from 'detritus-client-socket';

Media.Socket;
EventEmitterEventSpewerSocket
56 properties48 methodssrc/media.ts:88

Constructor

Properties

_heartbeat
{ ack: boolean; interval: Interval; intervalTime: number | null; lastAck: number | null; lastSent: number | null; nonce: number | null }
bucket
Bucket
channelId
string
connectedUserIds
Set<string>
dave
DaveSession | null
Default null
defaultWants
number
Default 100
endpoint
string | null
Default null
forceMode
Default null
gateway
helloTimeout
Timeout
identified
boolean
Default false
ipDiscoveryFailures
number
Default 0
keyframeInterval
number | null
Default null
killed
boolean
Default false
lastHeartbeatAckAt
number | null
Default null
mediaSinkWantsTimeout
Timeout
newServerTimeout
Timeout
promises
Set<{ reject: Function; resolve: Function }>
protocol
Default null
ready
boolean
Default false
receiveEnabled
boolean
Default false
reconnects
number
Default 0
remoteMediaSinkWants
MediaSinkWants | null
Default null
requestedNewServer
boolean
Default false
rtxSSRCs
Map<number, number>
selfDeaf
boolean
Default false
selfMute
boolean
Default false
selfVideo
boolean
Default false
sentMediaSinkWants
string | null
Default null
sequence
number
Default -1
serverId
string
serverVersion
number
Default 0
socket
BaseSocket | null
Default null
ssrcs
{ audio: Map<number, string>; video: Map<number, string> }
state
readonly
Default SocketStates.CLOSED
streams
Stream[]
Default []
token
string | null
Default null
transport
Socket | null
Default null
userId
string
userStreams
Map<string, Stream[]>
userWants
Map<string, number>
videoActive
boolean
Default false
videoEnabled
boolean
videoStreamSettings
Default {}
voiceBackendVersion
{ rtcWorker: string; voice: string } | null
Default null

Accessors

audioSSRC
number
get
closed
boolean
get
closing
boolean
get
connected
boolean
get
connecting
boolean
get
guildId
string | null
get
inDm
boolean
get
rtxSSRC
number
get
sessionId
string | null
get
transportReady
boolean
get

The udp transport has its key and audio can be sent, unlike state being READY An e2ee call still drops frames until dave.canEncrypt

videoSSRC
number
get
voicePrivacyCode
string | null
get

Methods

buildMediaSinkWants()

src/media.ts:1314
buildMediaSinkWants(): MediaSinkWants

The wants for our current receive, video, defaultWants and userWants settings. Users are only listed once we know their ssrc, until then any applies to them.

disconnect()

src/media.ts:489
disconnect(code?: number, reason?: string): void
code?
number
Default SocketCloseCodes.NORMAL
reason?
string

heartbeat()

src/media.ts:1138
heartbeat(fromInterval?: boolean): void
fromInterval?
boolean
Default false

kill()

src/media.ts:897
kill(error?: any, options?: { voiceStateUpdate?: boolean }): void
error?
any
options?
{ voiceStateUpdate?: boolean }
options.voiceStateUpdate?
boolean

on()

src/media.ts:1492
on(event: string | symbol, listener: (...args: any[]) => void): this

Adds the listener function to the end of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.

server.on('connection', (stream) => {
  console.log('someone connected!');
});

Returns a reference to the EventEmitter, so that calls can be chained.

By default, event listeners are invoked in the order they are added. The emitter.prependListener() method can be used as an alternative to add the event listener to the beginning of the listeners array.

import { EventEmitter } from 'node:events';
const myEE = new EventEmitter();
myEE.on('foo', () => console.log('a'));
myEE.prependListener('foo', () => console.log('b'));
myEE.emit('foo');
// Prints:
//   b
//   a

Since v0.1.101

event
string | symbol
listener
(...args: any[]) => void

The callback function

on(event: "close", listener: (payload: { code: number; reason: string }) => any): this
on(event: "flags", listener: (payload: { flags: number; userId: string }) => any): this
on(event: "killed", listener: (payload: { error?: Error }) => any): this
on(event: "mediaSinkWants", listener: (wants: MediaSinkWants) => any): this
on(event: "open", listener: (target: BaseSocket) => any): this
on(event: "packet", listener: (packet: Packet) => any): this
on(
  event: "platform",
  listener: (payload: { platform: number; userId: string }) => any,
): this
on(event: "ready", listener: () => any): this
on(event: "socket", listener: (socket: BaseSocket) => any): this
on(event: "state", listener: (__namedParameters: { state: SocketStates }) => any): this
on(event: "transport", listener: (transport: Socket) => any): this
on(event: "transportReady", listener: (transport: Socket) => any): this
on(
  event: "videoQuality",
  listener: (payload: { quality: string; ssrc: number; userId: string }) => any,
): this
on(
  event: "voiceBackendVersion",
  listener: (payload: { rtcWorker: string; voice: string }) => any,
): this
on(event: "warn", listener: (error: Error) => any): this
Overrides EventSpewer.on

onClose()

src/media.ts:923
onClose(target: BaseSocket, event: { code: number; reason: string }): void
target
BaseSocket
event
{ code: number; reason: string }
event.code
number
event.reason
string

onMessage()

src/media.ts:1010
onMessage(target: BaseSocket, event: { data: any; type: string }): void
target
BaseSocket
event
{ data: any; type: string }
event.data
any
event.type
string

requestNewServer()

src/media.ts:875
requestNewServer(): void

The voice server stopped answering, asks discord for another one by sending our voice state again Once per server, if the new one fails too we die

resetSession()

src/media.ts:432
resetSession(): void

Forgets everything tied to the voice session, the next connection identifies instead of resuming

rtxToSSRC()

src/media.ts:384
rtxToSSRC(rtxSSRC: number): number | null

The video ssrc a retransmission (rtx) ssrc resends packets for

scheduleMediaSinkWants()

src/media.ts:1366
scheduleMediaSinkWants(): void

Like syncMediaSinkWants() but waits a moment, so setting the wants of many users at once sends one packet.

send()

src/media.ts:1071
send(op: number, d: any, callback?: Function, direct?: boolean): void

The callback always gets called once, with an error if the packet never went out Packets sent while disconnected are dropped, not queued

op
number
d
any
callback?
Function
direct?
boolean
Default false

sendBinary()

src/media.ts:1118
sendBinary(op: number, payload: Buffer, callback?: Function): void

sendClientConnect()

src/media.ts:1261
sendClientConnect(
  options?: { videoActive?: boolean } & VideoStreamSettings,
  callback?: Function,
): void

Tells the voice server (and through it everyone in the call) our ssrcs and if we're sending video The video settings are kept, so later calls only need videoActive

sendDaveTransitionReady()

src/media.ts:1134
sendDaveTransitionReady(transitionId: number, callback?: Function): void

sendMediaSinkWants()

src/media.ts:1303
sendMediaSinkWants(wants: MediaSinkWants, callback?: Function): void

sendSelectProtocol()

src/media.ts:1412
sendSelectProtocol(
  options: { codecs?: any[]; data: { address: string; mode: string; port: number }; experiments?: any[]; protocol?: string; rtcConnectionId?: string },
  callback?: Function,
): void
options
{ codecs?: any[]; data: { address: string; mode: string; port: number }; experiments?: any[]; protocol?: string; rtcConnectionId?: string }
options.codecs?
any[]
options.data
{ address: string; mode: string; port: number }
options.experiments?
any[]
options.protocol?
string
options.rtcConnectionId?
string
callback?
Function

sendSessionUpdate()

src/media.ts:1405
sendSessionUpdate(options: { codecs: any[] }, callback?: Function): void

Gives the voice server a new list of codecs mid call, it answers with a SESSION_UPDATE if that changes what we send

options
{ codecs: any[] }
options.codecs
any[]
callback?
Function

sendSpeaking()

src/media.ts:1439
sendSpeaking(
  options: { delay?: number; hidden?: boolean; priority?: boolean; soundshare?: boolean; ssrc?: number; voice?: boolean },
  callback?: Function,
): void
options
{ delay?: number; hidden?: boolean; priority?: boolean; soundshare?: boolean; ssrc?: number; voice?: boolean }
options.delay?
number
options.hidden?
boolean
options.priority?
boolean
options.soundshare?
boolean
options.ssrc?
number
options.voice?
boolean
callback?
Function

sendStateUpdate()

src/media.ts:1476
sendStateUpdate(
  options?: { selfDeaf?: boolean; selfMute?: boolean; selfVideo?: boolean },
  callback?: Function,
): void
options?
{ selfDeaf?: boolean; selfMute?: boolean; selfVideo?: boolean }
options.selfDeaf?
boolean
options.selfMute?
boolean
options.selfVideo?
boolean
callback?
Function

setDefaultWants()

src/media.ts:1385
setDefaultWants(value: number): void

Sets what we want from users without their own value, 0 to 100. 0 makes setUserWants() an allowlist, only the users given a value are forwarded to us.

setReceive()

src/media.ts:1376
setReceive(receive: boolean): void

Turns receiving on or off while connected. Off asks the server to stop forwarding everything to us.

setUserWants()

src/media.ts:1393
setUserWants(userId: string, value: number | null): void

Sets what we want from one user, 0 to 100. 0 stops the server from forwarding them to us, null removes the value.

syncMediaSinkWants()

src/media.ts:1343
syncMediaSinkWants(force?: boolean): void

Sends our wants to the server if they changed since the last time. Nothing is sent while the server's defaults are what we want, it forwards everything it can to us (every user's audio, and their video too when we identified with video).

force?
boolean
Default false

waitForNewServer()

src/media.ts:890
waitForNewServer(reason: string): void

Kills us if discord sends no VOICE_SERVER_UPDATE with an endpoint in time, setToken() stops it once one comes