Add VoiceConnection.disconnect([reason]);

This commit is contained in:
Amish Shah
2016-08-23 21:47:37 +01:00
parent f9a553a7f0
commit 328f3c4ae8
6 changed files with 80 additions and 3 deletions

File diff suppressed because one or more lines are too long

View File

@@ -40,6 +40,9 @@ class ClientVoiceManager {
const { channel, token, sessionID, endpoint, resolve, reject } = pendingRequest; const { channel, token, sessionID, endpoint, resolve, reject } = pendingRequest;
const voiceConnection = new VoiceConnection(this, channel, token, sessionID, endpoint, resolve, reject); const voiceConnection = new VoiceConnection(this, channel, token, sessionID, endpoint, resolve, reject);
this.connections.set(guildID, voiceConnection); this.connections.set(guildID, voiceConnection);
voiceConnection.on('disconnected', () => {
this.connections.delete(guildID);
});
} }
} }

View File

@@ -1,5 +1,6 @@
const VoiceConnectionWebSocket = require('./VoiceConnectionWebSocket'); const VoiceConnectionWebSocket = require('./VoiceConnectionWebSocket');
const VoiceConnectionUDPClient = require('./VoiceConnectionUDPClient'); const VoiceConnectionUDPClient = require('./VoiceConnectionUDPClient');
const Constants = require('../../util/Constants');
const EventEmitter = require('events').EventEmitter; const EventEmitter = require('events').EventEmitter;
/** /**
@@ -60,6 +61,38 @@ class VoiceConnection extends EventEmitter {
_onError(e) { _onError(e) {
this._reject(e); this._reject(e);
this.emit('error', e); this.emit('error', e);
this._shutdown(e);
}
/**
* Disconnects the Client from the Voice Channel
* @param {string} [reason='user requested'] the reason of the disconnection
* @returns {null}
*/
disconnect(reason = 'user requested') {
this.manager.client.ws.send({
op: Constants.OPCodes.VOICE_STATE_UPDATE,
d: {
guild_id: this.channel.guild.id,
channel_id: null,
self_mute: false,
self_deaf: false,
},
});
return this._shutdown(reason);
}
_onClose(e) {
return this._shutdown(e);
}
_shutdown(e) {
this.ready = false;
this.websocket._shutdown();
if (this.udp) {
this.udp._shutdown();
}
this.emit('disconnected', e);
} }
/** /**
@@ -69,9 +102,11 @@ class VoiceConnection extends EventEmitter {
*/ */
bindListeners() { bindListeners() {
this.websocket.on('error', err => this._onError(err)); this.websocket.on('error', err => this._onError(err));
this.websocket.on('close', err => this._onClose(err));
this.websocket.on('ready-for-udp', data => { this.websocket.on('ready-for-udp', data => {
this.udp = new VoiceConnectionUDPClient(this, data); this.udp = new VoiceConnectionUDPClient(this, data);
this.udp.on('error', err => this._onError(err)); this.udp.on('error', err => this._onError(err));
this.udp.on('close', err => this._onClose(err));
}); });
this.websocket.on('ready', () => { this.websocket.on('ready', () => {
this.ready = true; this.ready = true;

View File

@@ -32,6 +32,19 @@ class VoiceConnectionUDPClient extends EventEmitter {
} }
} }
_shutdown() {
if (this.udpSocket) {
try {
this.udpSocket.close();
} catch (err) {
if (err.message !== 'Not running') {
this.emit('error', err);
}
}
this.udpSocket = null;
}
}
connectUDP(address) { connectUDP(address) {
this.udpIP = address; this.udpIP = address;
this.udpSocket = udp.createSocket('udp4'); this.udpSocket = udp.createSocket('udp4');
@@ -64,6 +77,10 @@ class VoiceConnectionUDPClient extends EventEmitter {
this.emit('error', { error, message }); this.emit('error', { error, message });
}); });
this.udpSocket.on('close', error => {
this.emit('close', error);
});
const blankMessage = new Buffer(70); const blankMessage = new Buffer(70);
blankMessage.writeUIntBE(this.data.ssrc, 0, 4); blankMessage.writeUIntBE(this.data.ssrc, 0, 4);
this.send(blankMessage); this.send(blankMessage);

View File

@@ -12,6 +12,8 @@ class VoiceConnectionWebSocket extends EventEmitter {
this.ws = new WebSocket(`wss://${endpoint}`, null, { rejectUnauthorized: false }); this.ws = new WebSocket(`wss://${endpoint}`, null, { rejectUnauthorized: false });
this.ws.onopen = () => this._onOpen(); this.ws.onopen = () => this._onOpen();
this.ws.onmessage = e => this._onMessage(e); this.ws.onmessage = e => this._onMessage(e);
this.ws.onclose = e => this._onClose(e);
this.heartbeat = null;
} }
send(data) { send(data) {
@@ -20,6 +22,14 @@ class VoiceConnectionWebSocket extends EventEmitter {
} }
} }
_shutdown() {
if (this.ws) {
this.ws.close();
this.ws = null;
}
clearInterval(this.heartbeat);
}
_onOpen() { _onOpen() {
this.send({ this.send({
op: Constants.OPCodes.DISPATCH, op: Constants.OPCodes.DISPATCH,
@@ -32,8 +42,20 @@ class VoiceConnectionWebSocket extends EventEmitter {
}); });
} }
_onClose(e) {
this.emit('close', e);
}
_onError(e) { _onError(e) {
throw e; this.emit('error', e);
}
_setHeartbeat(interval) {
this.heartbeat = setInterval(() => {
this.send({
op: Constants.VoiceOPCodes.HEARTBEAT,
});
}, interval);
} }
_onMessage(event) { _onMessage(event) {
@@ -46,6 +68,7 @@ class VoiceConnectionWebSocket extends EventEmitter {
switch (packet.op) { switch (packet.op) {
case Constants.VoiceOPCodes.READY: case Constants.VoiceOPCodes.READY:
this._setHeartbeat(packet.d.heartbeat_interval);
this.emit('ready-for-udp', packet.d); this.emit('ready-for-udp', packet.d);
break; break;
case Constants.VoiceOPCodes.SESSION_DESCRIPTION: case Constants.VoiceOPCodes.SESSION_DESCRIPTION:

View File

@@ -127,7 +127,6 @@ class WebSocketManager {
* @returns {null} * @returns {null}
*/ */
eventClose(event) { eventClose(event) {
console.log('close', event.code);
if (event.code === 4004) { if (event.code === 4004) {
throw Constants.Errors.BAD_LOGIN; throw Constants.Errors.BAD_LOGIN;
} }