mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-13 10:03:31 +01:00
finish up voice websocket for now
This commit is contained in:
@@ -16,6 +16,11 @@ class VoiceWebSocket extends EventEmitter {
|
|||||||
* @type {VoiceConnection}
|
* @type {VoiceConnection}
|
||||||
*/
|
*/
|
||||||
this.voiceConnection = voiceConnection;
|
this.voiceConnection = voiceConnection;
|
||||||
|
/**
|
||||||
|
* How many connection attempts have been made
|
||||||
|
* @type {number}
|
||||||
|
*/
|
||||||
|
this.attempts = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -27,13 +32,31 @@ class VoiceWebSocket extends EventEmitter {
|
|||||||
return this.voiceConnection.voiceManager.client;
|
return this.voiceConnection.voiceManager.client;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resets the current WebSocket
|
||||||
|
*/
|
||||||
|
reset() {
|
||||||
|
if (this.ws) {
|
||||||
|
if (this.ws.readyState !== WebSocket.CLOSED) {
|
||||||
|
this.ws.close();
|
||||||
|
}
|
||||||
|
this.ws = null;
|
||||||
|
}
|
||||||
|
this.clearHeartbeat();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts connecting to the Voice WebSocket Server.
|
* Starts connecting to the Voice WebSocket Server.
|
||||||
*/
|
*/
|
||||||
connect() {
|
connect() {
|
||||||
if (this.ws) {
|
if (this.ws) {
|
||||||
throw new Error('there is already an existing websocket');
|
this.reset();
|
||||||
}
|
}
|
||||||
|
if (this.attempts > 5) {
|
||||||
|
this.emit('error', new Error(`too many connection attempts (${this.attempts})`));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.attempts++;
|
||||||
/**
|
/**
|
||||||
* 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}
|
||||||
@@ -110,6 +133,23 @@ class VoiceWebSocket extends EventEmitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called whenever the connection to the WebSocket Server is lost
|
||||||
|
* @param {CloseEvent} event the close event
|
||||||
|
*/
|
||||||
|
onClose(event) {
|
||||||
|
// #todo see if the connection is open before reconnecting
|
||||||
|
this.client.setTimeout(this.connect.bind(this), this.attempts * 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called whenever an error occurs with the WebSocket.
|
||||||
|
* @param {Error} error the error that occurred
|
||||||
|
*/
|
||||||
|
onError(error) {
|
||||||
|
this.emit('error', error);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called whenever a valid packet is received from the WebSocket
|
* Called whenever a valid packet is received from the WebSocket
|
||||||
* @param {Object} packet the received packet
|
* @param {Object} packet the received packet
|
||||||
@@ -118,6 +158,12 @@ class VoiceWebSocket extends EventEmitter {
|
|||||||
switch (packet.op) {
|
switch (packet.op) {
|
||||||
case Constants.VoiceOPCodes.READY:
|
case Constants.VoiceOPCodes.READY:
|
||||||
this.setHeartbeat(packet.d.heartbeat_interval);
|
this.setHeartbeat(packet.d.heartbeat_interval);
|
||||||
|
/**
|
||||||
|
* Emitted once the voice websocket receives the ready packet
|
||||||
|
* @param {Object} packet the received packet
|
||||||
|
* @event VoiceWebSocket#ready
|
||||||
|
*/
|
||||||
|
this.emit('ready', packet.d);
|
||||||
break;
|
break;
|
||||||
case Constants.VoiceOPCodes.SESSION_DESCRIPTION:
|
case Constants.VoiceOPCodes.SESSION_DESCRIPTION:
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user