Skip to content

Commit d40ceed

Browse files
feat(voice): use voice gateway v8 (#10918)
* feat(voice): use voice gateway v8 * docs: small typo --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent e094faf commit d40ceed

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

packages/voice/src/networking/Networking.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,14 @@ export class Networking extends EventEmitter {
298298
* Creates a new WebSocket to a Discord Voice gateway.
299299
*
300300
* @param endpoint - The endpoint to connect to
301+
* @param lastSequence - The last sequence to set for this WebSocket
301302
*/
302-
private createWebSocket(endpoint: string) {
303-
const ws = new VoiceWebSocket(`wss://${endpoint}?v=4`, Boolean(this.debug));
303+
private createWebSocket(endpoint: string, lastSequence?: number) {
304+
const ws = new VoiceWebSocket(`wss://${endpoint}?v=8`, Boolean(this.debug));
305+
306+
if (lastSequence !== undefined) {
307+
ws.sequence = lastSequence;
308+
}
304309

305310
ws.on('error', this.onChildError);
306311
ws.once('open', this.onWsOpen);
@@ -347,6 +352,7 @@ export class Networking extends EventEmitter {
347352
server_id: this.state.connectionOptions.serverId,
348353
session_id: this.state.connectionOptions.sessionId,
349354
token: this.state.connectionOptions.token,
355+
seq_ack: this.state.ws.sequence,
350356
},
351357
};
352358
this.state.ws.sendPacket(packet);
@@ -363,10 +369,11 @@ export class Networking extends EventEmitter {
363369
private onWsClose({ code }: CloseEvent) {
364370
const canResume = code === 4_015 || code < 4_000;
365371
if (canResume && this.state.code === NetworkingStatusCode.Ready) {
372+
const lastSequence = this.state.ws.sequence;
366373
this.state = {
367374
...this.state,
368375
code: NetworkingStatusCode.Resuming,
369-
ws: this.createWebSocket(this.state.connectionOptions.endpoint),
376+
ws: this.createWebSocket(this.state.connectionOptions.endpoint, lastSequence),
370377
};
371378
} else if (this.state.code !== NetworkingStatusCode.Closed) {
372379
this.destroy();
@@ -379,10 +386,11 @@ export class Networking extends EventEmitter {
379386
*/
380387
private onUdpClose() {
381388
if (this.state.code === NetworkingStatusCode.Ready) {
389+
const lastSequence = this.state.ws.sequence;
382390
this.state = {
383391
...this.state,
384392
code: NetworkingStatusCode.Resuming,
385-
ws: this.createWebSocket(this.state.connectionOptions.endpoint),
393+
ws: this.createWebSocket(this.state.connectionOptions.endpoint, lastSequence),
386394
};
387395
}
388396
}

packages/voice/src/networking/VoiceWebSocket.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ export class VoiceWebSocket extends EventEmitter {
5252
*/
5353
public ping?: number;
5454

55+
/**
56+
* The last sequence number acknowledged from Discord. Will be `-1` if no sequence numbered messages have been received.
57+
*/
58+
public sequence = -1;
59+
5560
/**
5661
* The debug logger function, if debugging is enabled.
5762
*/
@@ -115,6 +120,10 @@ export class VoiceWebSocket extends EventEmitter {
115120
return;
116121
}
117122

123+
if (packet.seq) {
124+
this.sequence = packet.seq;
125+
}
126+
118127
if (packet.op === VoiceOpcodes.HeartbeatAck) {
119128
this.lastHeartbeatAck = Date.now();
120129
this.missedHeartbeats = 0;
@@ -150,7 +159,11 @@ export class VoiceWebSocket extends EventEmitter {
150159
this.sendPacket({
151160
op: VoiceOpcodes.Heartbeat,
152161
// eslint-disable-next-line id-length
153-
d: nonce,
162+
d: {
163+
// eslint-disable-next-line id-length
164+
t: nonce,
165+
seq_ack: this.sequence,
166+
},
154167
});
155168
}
156169

0 commit comments

Comments
 (0)