voice: handle new client_connect and client_disconnect packets

This commit is contained in:
Amish Shah
2018-06-22 18:38:33 +01:00
parent 9296a30148
commit 08bbbe9301
4 changed files with 27 additions and 14 deletions

View File

@@ -429,7 +429,7 @@ class VoiceConnection extends EventEmitter {
speaking = Boolean(speaking);
const guild = this.channel.guild;
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.
* @event VoiceConnection#speaking

View File

@@ -9,20 +9,20 @@ const WebSocket = require('../../../WebSocket');
* @private
*/
class VoiceWebSocket extends EventEmitter {
constructor(voiceConnection) {
constructor(connection) {
super();
/**
* The client of this voice WebSocket
* @type {Client}
*/
this.client = voiceConnection.voiceManager.client;
this.client = connection.voiceManager.client;
/**
* The Voice Connection that this WebSocket serves
* @type {VoiceConnection}
*/
this.voiceConnection = voiceConnection;
this.connection = connection;
/**
* How many connection attempts have been made
@@ -32,7 +32,7 @@ class VoiceWebSocket extends EventEmitter {
this.connect();
this.dead = false;
this.voiceConnection.on('closing', this.shutdown.bind(this));
this.connection.on('closing', this.shutdown.bind(this));
}
shutdown() {
@@ -68,7 +68,7 @@ class VoiceWebSocket extends EventEmitter {
* The actual WebSocket used to connect to the Voice WebSocket Server.
* @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.onmessage = this.onMessage.bind(this);
this.ws.onclose = this.onClose.bind(this);
@@ -110,10 +110,10 @@ class VoiceWebSocket extends EventEmitter {
this.sendPacket({
op: OPCodes.DISPATCH,
d: {
server_id: this.voiceConnection.channel.guild.id,
server_id: this.connection.channel.guild.id,
user_id: this.client.user.id,
token: this.voiceConnection.authentication.token,
session_id: this.voiceConnection.authentication.sessionID,
token: this.connection.authentication.token,
session_id: this.connection.authentication.sessionID,
},
}).catch(() => {
this.emit('error', new Error('VOICE_JOIN_SOCKET_CLOSED'));
@@ -177,6 +177,15 @@ class VoiceWebSocket extends EventEmitter {
*/
this.emit('sessionDescription', packet.d.mode, key);
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:
/**
* Emitted whenever a speaking packet is received.

View File

@@ -11,11 +11,13 @@ class PacketHandler extends EventEmitter {
this.streams = new Map();
}
get connection() {
return this.receiver.connection;
}
_stoppedSpeaking(userID) {
if (this.streams.has(userID)) {
const { stream, end } = this.streams.get(userID);
if (end === 'silence') stream.push(null);
}
const streamInfo = this.streams.get(userID);
if (streamInfo && streamInfo.end === 'silence') streamInfo.stream.push(null);
}
makeStream(user, end) {
@@ -63,7 +65,7 @@ class PacketHandler extends EventEmitter {
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) {
const ssrc = buffer.readUInt32BE(8);

View File

@@ -198,6 +198,8 @@ exports.VoiceOPCodes = {
SESSION_DESCRIPTION: 4,
SPEAKING: 5,
HELLO: 8,
CLIENT_CONNECT: 12,
CLIENT_DISCONNECT: 13,
};
exports.Events = {