mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-16 03:23:29 +01:00
voice: handle new client_connect and client_disconnect packets
This commit is contained in:
@@ -429,7 +429,7 @@ class VoiceConnection extends EventEmitter {
|
|||||||
speaking = Boolean(speaking);
|
speaking = Boolean(speaking);
|
||||||
const guild = this.channel.guild;
|
const guild = this.channel.guild;
|
||||||
const user = this.client.users.get(user_id);
|
const user = this.client.users.get(user_id);
|
||||||
this.ssrcMap.set(+ssrc, user);
|
this.ssrcMap.set(+ssrc, user_id);
|
||||||
/**
|
/**
|
||||||
* Emitted whenever a user starts/stops speaking.
|
* Emitted whenever a user starts/stops speaking.
|
||||||
* @event VoiceConnection#speaking
|
* @event VoiceConnection#speaking
|
||||||
|
|||||||
@@ -9,20 +9,20 @@ const WebSocket = require('../../../WebSocket');
|
|||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
class VoiceWebSocket extends EventEmitter {
|
class VoiceWebSocket extends EventEmitter {
|
||||||
constructor(voiceConnection) {
|
constructor(connection) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The client of this voice WebSocket
|
* The client of this voice WebSocket
|
||||||
* @type {Client}
|
* @type {Client}
|
||||||
*/
|
*/
|
||||||
this.client = voiceConnection.voiceManager.client;
|
this.client = connection.voiceManager.client;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The Voice Connection that this WebSocket serves
|
* The Voice Connection that this WebSocket serves
|
||||||
* @type {VoiceConnection}
|
* @type {VoiceConnection}
|
||||||
*/
|
*/
|
||||||
this.voiceConnection = voiceConnection;
|
this.connection = connection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* How many connection attempts have been made
|
* How many connection attempts have been made
|
||||||
@@ -32,7 +32,7 @@ class VoiceWebSocket extends EventEmitter {
|
|||||||
|
|
||||||
this.connect();
|
this.connect();
|
||||||
this.dead = false;
|
this.dead = false;
|
||||||
this.voiceConnection.on('closing', this.shutdown.bind(this));
|
this.connection.on('closing', this.shutdown.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
shutdown() {
|
shutdown() {
|
||||||
@@ -68,7 +68,7 @@ class VoiceWebSocket extends EventEmitter {
|
|||||||
* The actual WebSocket used to connect to the Voice WebSocket Server.
|
* The actual WebSocket used to connect to the Voice WebSocket Server.
|
||||||
* @type {WebSocket}
|
* @type {WebSocket}
|
||||||
*/
|
*/
|
||||||
this.ws = WebSocket.create(`wss://${this.voiceConnection.authentication.endpoint}/`, { v: 4 });
|
this.ws = WebSocket.create(`wss://${this.connection.authentication.endpoint}/`, { v: 4 });
|
||||||
this.ws.onopen = this.onOpen.bind(this);
|
this.ws.onopen = this.onOpen.bind(this);
|
||||||
this.ws.onmessage = this.onMessage.bind(this);
|
this.ws.onmessage = this.onMessage.bind(this);
|
||||||
this.ws.onclose = this.onClose.bind(this);
|
this.ws.onclose = this.onClose.bind(this);
|
||||||
@@ -110,10 +110,10 @@ class VoiceWebSocket extends EventEmitter {
|
|||||||
this.sendPacket({
|
this.sendPacket({
|
||||||
op: OPCodes.DISPATCH,
|
op: OPCodes.DISPATCH,
|
||||||
d: {
|
d: {
|
||||||
server_id: this.voiceConnection.channel.guild.id,
|
server_id: this.connection.channel.guild.id,
|
||||||
user_id: this.client.user.id,
|
user_id: this.client.user.id,
|
||||||
token: this.voiceConnection.authentication.token,
|
token: this.connection.authentication.token,
|
||||||
session_id: this.voiceConnection.authentication.sessionID,
|
session_id: this.connection.authentication.sessionID,
|
||||||
},
|
},
|
||||||
}).catch(() => {
|
}).catch(() => {
|
||||||
this.emit('error', new Error('VOICE_JOIN_SOCKET_CLOSED'));
|
this.emit('error', new Error('VOICE_JOIN_SOCKET_CLOSED'));
|
||||||
@@ -177,6 +177,15 @@ class VoiceWebSocket extends EventEmitter {
|
|||||||
*/
|
*/
|
||||||
this.emit('sessionDescription', packet.d.mode, key);
|
this.emit('sessionDescription', packet.d.mode, key);
|
||||||
break;
|
break;
|
||||||
|
case VoiceOPCodes.CLIENT_CONNECT:
|
||||||
|
this.connection.ssrcMap.set(+packet.d.audio_ssrc, packet.d.user_id);
|
||||||
|
break;
|
||||||
|
case VoiceOPCodes.CLIENT_DISCONNECT:
|
||||||
|
for (const receiver of this.connection.receivers) {
|
||||||
|
const streamInfo = receiver.packets.streams.get(packet.d.user_id);
|
||||||
|
if (streamInfo) streamInfo.stream.push(null);
|
||||||
|
}
|
||||||
|
break;
|
||||||
case VoiceOPCodes.SPEAKING:
|
case VoiceOPCodes.SPEAKING:
|
||||||
/**
|
/**
|
||||||
* Emitted whenever a speaking packet is received.
|
* Emitted whenever a speaking packet is received.
|
||||||
|
|||||||
@@ -11,11 +11,13 @@ class PacketHandler extends EventEmitter {
|
|||||||
this.streams = new Map();
|
this.streams = new Map();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get connection() {
|
||||||
|
return this.receiver.connection;
|
||||||
|
}
|
||||||
|
|
||||||
_stoppedSpeaking(userID) {
|
_stoppedSpeaking(userID) {
|
||||||
if (this.streams.has(userID)) {
|
const streamInfo = this.streams.get(userID);
|
||||||
const { stream, end } = this.streams.get(userID);
|
if (streamInfo && streamInfo.end === 'silence') streamInfo.stream.push(null);
|
||||||
if (end === 'silence') stream.push(null);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
makeStream(user, end) {
|
makeStream(user, end) {
|
||||||
@@ -63,7 +65,7 @@ class PacketHandler extends EventEmitter {
|
|||||||
return packet;
|
return packet;
|
||||||
}
|
}
|
||||||
|
|
||||||
userFromSSRC(ssrc) { return this.receiver.connection.ssrcMap.get(ssrc); }
|
userFromSSRC(ssrc) { return this.connection.client.users.get(this.connection.ssrcMap.get(ssrc)); }
|
||||||
|
|
||||||
push(buffer) {
|
push(buffer) {
|
||||||
const ssrc = buffer.readUInt32BE(8);
|
const ssrc = buffer.readUInt32BE(8);
|
||||||
|
|||||||
@@ -198,6 +198,8 @@ exports.VoiceOPCodes = {
|
|||||||
SESSION_DESCRIPTION: 4,
|
SESSION_DESCRIPTION: 4,
|
||||||
SPEAKING: 5,
|
SPEAKING: 5,
|
||||||
HELLO: 8,
|
HELLO: 8,
|
||||||
|
CLIENT_CONNECT: 12,
|
||||||
|
CLIENT_DISCONNECT: 13,
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.Events = {
|
exports.Events = {
|
||||||
|
|||||||
Reference in New Issue
Block a user