mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-18 12:33:30 +01:00
@@ -72,6 +72,8 @@ class ClientVoiceManager {
|
|||||||
reject(reason);
|
reject(reason);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
connection.on('debug', msg => this.client.emit('debug', `[VOICE (${channel.guild.id})]: ${msg}`));
|
||||||
|
|
||||||
connection.once('authenticated', () => {
|
connection.once('authenticated', () => {
|
||||||
connection.once('ready', () => {
|
connection.once('ready', () => {
|
||||||
resolve(connection);
|
resolve(connection);
|
||||||
|
|||||||
@@ -396,6 +396,8 @@ class VoiceConnection extends EventEmitter {
|
|||||||
|
|
||||||
const { ws, udp } = this.sockets;
|
const { ws, udp } = this.sockets;
|
||||||
|
|
||||||
|
ws.on('debug', msg => this.emit('debug', msg));
|
||||||
|
udp.on('debug', msg => this.emit('debug', msg));
|
||||||
ws.on('error', err => this.emit('error', err));
|
ws.on('error', err => this.emit('error', err));
|
||||||
udp.on('error', err => this.emit('error', err));
|
udp.on('error', err => this.emit('error', err));
|
||||||
ws.on('ready', this.onReady.bind(this));
|
ws.on('ready', this.onReady.bind(this));
|
||||||
@@ -431,6 +433,7 @@ class VoiceConnection extends EventEmitter {
|
|||||||
const dispatcher = this.play(new SingleSilence(), { type: 'opus' });
|
const dispatcher = this.play(new SingleSilence(), { type: 'opus' });
|
||||||
dispatcher.on('finish', () => {
|
dispatcher.on('finish', () => {
|
||||||
clearTimeout(this.connectTimeout);
|
clearTimeout(this.connectTimeout);
|
||||||
|
this.emit('debug', `Ready with authentication details: ${JSON.stringify(this.authentication)}`);
|
||||||
/**
|
/**
|
||||||
* Emitted once the connection is ready, when a promise to join a voice channel resolves,
|
* Emitted once the connection is ready, when a promise to join a voice channel resolves,
|
||||||
* the connection will already be ready.
|
* the connection will already be ready.
|
||||||
|
|||||||
@@ -48,6 +48,7 @@ class VoiceConnectionUDPClient extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
shutdown() {
|
shutdown() {
|
||||||
|
this.emit('debug', `[UDP] shutdown requested`);
|
||||||
if (this.socket) {
|
if (this.socket) {
|
||||||
this.socket.removeAllListeners('message');
|
this.socket.removeAllListeners('message');
|
||||||
try {
|
try {
|
||||||
@@ -77,7 +78,12 @@ class VoiceConnectionUDPClient extends EventEmitter {
|
|||||||
if (!this.socket) throw new Error('UDP_SEND_FAIL');
|
if (!this.socket) throw new Error('UDP_SEND_FAIL');
|
||||||
if (!this.discordAddress || !this.discordPort) throw new Error('UDP_ADDRESS_MALFORMED');
|
if (!this.discordAddress || !this.discordPort) throw new Error('UDP_ADDRESS_MALFORMED');
|
||||||
this.socket.send(packet, 0, packet.length, this.discordPort, this.discordAddress, error => {
|
this.socket.send(packet, 0, packet.length, this.discordPort, this.discordAddress, error => {
|
||||||
if (error) reject(error); else resolve(packet);
|
if (error) {
|
||||||
|
this.emit('debug', `[UDP] >> ERROR: ${error}`);
|
||||||
|
reject(error);
|
||||||
|
} else {
|
||||||
|
resolve(packet);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -85,13 +91,14 @@ class VoiceConnectionUDPClient extends EventEmitter {
|
|||||||
createUDPSocket(address) {
|
createUDPSocket(address) {
|
||||||
this.discordAddress = address;
|
this.discordAddress = address;
|
||||||
const socket = this.socket = udp.createSocket('udp4');
|
const socket = this.socket = udp.createSocket('udp4');
|
||||||
|
this.emit('debug', `[UDP] created socket`);
|
||||||
socket.once('message', message => {
|
socket.once('message', message => {
|
||||||
// Stop if the sockets have been deleted because the connection has been closed already
|
// Stop if the sockets have been deleted because the connection has been closed already
|
||||||
if (!this.voiceConnection.sockets.ws) return;
|
if (!this.voiceConnection.sockets.ws) return;
|
||||||
|
|
||||||
const packet = parseLocalPacket(message);
|
const packet = parseLocalPacket(message);
|
||||||
if (packet.error) {
|
if (packet.error) {
|
||||||
|
this.emit('debug', `[UDP] ERROR: ${packet.error}`);
|
||||||
this.emit('error', packet.error);
|
this.emit('error', packet.error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -111,6 +118,8 @@ class VoiceConnectionUDPClient extends EventEmitter {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.emit('debug', `[UDP] << ${JSON.stringify(packet)}`);
|
||||||
|
|
||||||
socket.on('message', buffer => this.voiceConnection.receiver.packets.push(buffer));
|
socket.on('message', buffer => this.voiceConnection.receiver.packets.push(buffer));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ class VoiceWebSocket extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
shutdown() {
|
shutdown() {
|
||||||
|
this.emit('debug', `[WS] shutdown requested`);
|
||||||
this.dead = true;
|
this.dead = true;
|
||||||
this.reset();
|
this.reset();
|
||||||
}
|
}
|
||||||
@@ -47,6 +48,7 @@ class VoiceWebSocket extends EventEmitter {
|
|||||||
* Resets the current WebSocket.
|
* Resets the current WebSocket.
|
||||||
*/
|
*/
|
||||||
reset() {
|
reset() {
|
||||||
|
this.emit('debug', `[WS] reset requested`);
|
||||||
if (this.ws) {
|
if (this.ws) {
|
||||||
if (this.ws.readyState !== WebSocket.CLOSED) this.ws.close();
|
if (this.ws.readyState !== WebSocket.CLOSED) this.ws.close();
|
||||||
this.ws = null;
|
this.ws = null;
|
||||||
@@ -58,6 +60,7 @@ class VoiceWebSocket extends EventEmitter {
|
|||||||
* Starts connecting to the Voice WebSocket Server.
|
* Starts connecting to the Voice WebSocket Server.
|
||||||
*/
|
*/
|
||||||
connect() {
|
connect() {
|
||||||
|
this.emit('debug', `[WS] connect requested`);
|
||||||
if (this.dead) return;
|
if (this.dead) return;
|
||||||
if (this.ws) this.reset();
|
if (this.ws) this.reset();
|
||||||
if (this.attempts >= 5) {
|
if (this.attempts >= 5) {
|
||||||
@@ -66,6 +69,7 @@ class VoiceWebSocket extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.attempts++;
|
this.attempts++;
|
||||||
|
this.emit('debug', `[WS] connecting with ${this.attempts} attempts`);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The actual WebSocket used to connect to the Voice WebSocket Server.
|
* The actual WebSocket used to connect to the Voice WebSocket Server.
|
||||||
@@ -84,6 +88,7 @@ class VoiceWebSocket extends EventEmitter {
|
|||||||
* @returns {Promise<string>}
|
* @returns {Promise<string>}
|
||||||
*/
|
*/
|
||||||
send(data) {
|
send(data) {
|
||||||
|
this.emit('debug', `[WS] >> ${data}`);
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) throw new Error('WS_NOT_OPEN', data);
|
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) throw new Error('WS_NOT_OPEN', data);
|
||||||
this.ws.send(data, null, error => {
|
this.ws.send(data, null, error => {
|
||||||
@@ -110,6 +115,7 @@ class VoiceWebSocket extends EventEmitter {
|
|||||||
* Called whenever the WebSocket opens.
|
* Called whenever the WebSocket opens.
|
||||||
*/
|
*/
|
||||||
onOpen() {
|
onOpen() {
|
||||||
|
this.emit('debug', `[WS] opened at gateway ${this.connection.authentication.endpoint}`);
|
||||||
this.sendPacket({
|
this.sendPacket({
|
||||||
op: OPCodes.DISPATCH,
|
op: OPCodes.DISPATCH,
|
||||||
d: {
|
d: {
|
||||||
@@ -140,6 +146,7 @@ class VoiceWebSocket extends EventEmitter {
|
|||||||
* Called whenever the connection to the WebSocket server is lost.
|
* Called whenever the connection to the WebSocket server is lost.
|
||||||
*/
|
*/
|
||||||
onClose() {
|
onClose() {
|
||||||
|
this.emit('debug', `[WS] closed`);
|
||||||
if (!this.dead) this.client.setTimeout(this.connect.bind(this), this.attempts * 1000);
|
if (!this.dead) this.client.setTimeout(this.connect.bind(this), this.attempts * 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -156,6 +163,7 @@ class VoiceWebSocket extends EventEmitter {
|
|||||||
* @param {Object} packet The received packet
|
* @param {Object} packet The received packet
|
||||||
*/
|
*/
|
||||||
onPacket(packet) {
|
onPacket(packet) {
|
||||||
|
this.emit('debug', `[WS] << ${JSON.stringify(packet)}`);
|
||||||
switch (packet.op) {
|
switch (packet.op) {
|
||||||
case VoiceOPCodes.HELLO:
|
case VoiceOPCodes.HELLO:
|
||||||
this.setHeartbeat(packet.d.heartbeat_interval);
|
this.setHeartbeat(packet.d.heartbeat_interval);
|
||||||
|
|||||||
Reference in New Issue
Block a user