Skip to content

Commit

Permalink
feat(voice): Support resuming voice connection (#1448)
Browse files Browse the repository at this point in the history
Co-authored-by: bsian03 <[email protected]>
  • Loading branch information
james58899 and bsian03 authored Jul 9, 2024
1 parent 55a8a69 commit 552dbb0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4234,6 +4234,7 @@ declare namespace Eris {
receiveStreamOpus?: VoiceDataStream | null;
receiveStreamPCM?: VoiceDataStream | null;
reconnecting: boolean;
resuming: boolean;
samplingRate: number;
secret: Buffer;
sendBuffer: Buffer;
Expand Down
35 changes: 29 additions & 6 deletions lib/voice/VoiceConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ converterCommand.pickCommand = function pickCommand() {
* @prop {Boolean} paused Whether the voice connection is paused
* @prop {Boolean} playing Whether the voice connection is playing something
* @prop {Boolean} ready Whether the voice connection is ready
* @prop {Boolean} resuming Whether the voice connection is waiting for resuming
* @prop {Number} volume The current volume level of the connection
*/
class VoiceConnection extends EventEmitter {
Expand Down Expand Up @@ -120,6 +121,7 @@ class VoiceConnection extends EventEmitter {
this.connectionTimeout = null;
this.connecting = false;
this.reconnecting = false;
this.resuming = false;
this.ready = false;

this.sendBuffer = Buffer.allocUnsafe(16 + 32 + MAX_FRAME_SIZE);
Expand Down Expand Up @@ -199,12 +201,20 @@ class VoiceConnection extends EventEmitter {
clearTimeout(this.connectionTimeout);
this.connectionTimeout = null;
}
this.sendWS(VoiceOPCodes.IDENTIFY, {
server_id: this.id === "call" ? data.channel_id : this.id,
user_id: data.user_id,
session_id: data.session_id,
token: data.token
});
if(this.resuming) {
this.sendWS(VoiceOPCodes.RESUME, {
server_id: this.id === "call" ? data.channel_id : this.id,
session_id: data.session_id,
token: data.token
});
} else {
this.sendWS(VoiceOPCodes.IDENTIFY, {
server_id: this.id === "call" ? data.channel_id : this.id,
user_id: data.user_id,
session_id: data.session_id,
token: data.token
});
}
});
this.ws.on("message", (m) => {
const packet = JSON.parse(m);
Expand Down Expand Up @@ -269,6 +279,11 @@ class VoiceConnection extends EventEmitter {
this.sendUDPPacket(udpMessage);
break;
}
case VoiceOPCodes.RESUMED: {
this.connecting = false;
this.resuming = false;
break;
}
case VoiceOPCodes.SESSION_DESCRIPTION: {
this.mode = packet.d.mode;
this.secret = Buffer.from(packet.d.secret_key);
Expand Down Expand Up @@ -355,6 +370,13 @@ class VoiceConnection extends EventEmitter {
this.emit("warn", `Voice WS close ${code}: ${reason}`);
if(this.connecting || this.ready) {
let reconnecting = true;
if(code < 4000 || code === 4015) {
this.resuming = true;
setTimeout(() => {
this.connect(data);
}, 500).unref();
return;
}
if(code === 4006) {
reconnecting = false;
} else if(code === 4014) {
Expand Down Expand Up @@ -383,6 +405,7 @@ class VoiceConnection extends EventEmitter {
disconnect(error, reconnecting) {
this.connecting = false;
this.reconnecting = reconnecting;
this.resuming = false;
this.ready = false;
this.speaking = false;
this.timestamp = 0;
Expand Down

0 comments on commit 552dbb0

Please sign in to comment.