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

View File

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