mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-14 10:33:30 +01:00
Add voice websocket handling
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -1,6 +1,7 @@
|
||||
const Collection = require('../../util/Collection');
|
||||
const mergeDefault = require('../../util/MergeDefault');
|
||||
const Constants = require('../../util/Constants');
|
||||
const VoiceConnection = require('./VoiceConnection');
|
||||
|
||||
/**
|
||||
* Manages all the voice stuff for the Client
|
||||
@@ -31,7 +32,9 @@ class ClientVoiceManager {
|
||||
throw new Error('Guild not pending');
|
||||
}
|
||||
if (pendingRequest.token && pendingRequest.sessionID && pendingRequest.endpoint) {
|
||||
console.log('got all info for', guildID);
|
||||
const { token, sessionID, endpoint } = pendingRequest;
|
||||
const voiceConnection = new VoiceConnection(this, guildID, token, sessionID, endpoint);
|
||||
this.connections.set(guildID, voiceConnection);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,7 +50,8 @@ class ClientVoiceManager {
|
||||
throw new Error('Guild not pending');
|
||||
}
|
||||
pendingRequest.token = token;
|
||||
pendingRequest.endpoint = endpoint;
|
||||
// remove the port otherwise it errors ¯\_(ツ)_/¯
|
||||
pendingRequest.endpoint = endpoint.match(/([^:]*)/)[0];
|
||||
this._checkPendingReady(guildID);
|
||||
}
|
||||
|
||||
|
||||
19
src/client/voice/VoiceConnection.js
Normal file
19
src/client/voice/VoiceConnection.js
Normal file
@@ -0,0 +1,19 @@
|
||||
const VoiceConnectionWebSocket = require('./VoiceConnectionWebSocket');
|
||||
const EventEmitter = require('events').EventEmitter;
|
||||
|
||||
class VoiceConnection extends EventEmitter {
|
||||
constructor(manager, serverID, token, sessionID, endpoint) {
|
||||
super();
|
||||
this.manager = manager;
|
||||
this.websocket = new VoiceConnectionWebSocket(this, serverID, token, sessionID, endpoint);
|
||||
this.bindListeners();
|
||||
}
|
||||
|
||||
bindListeners() {
|
||||
this.websocket.on('ready-for-udp', data => {
|
||||
console.log(data);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = VoiceConnection;
|
||||
5
src/client/voice/VoiceConnectionUDPClient.js
Normal file
5
src/client/voice/VoiceConnectionUDPClient.js
Normal file
@@ -0,0 +1,5 @@
|
||||
class VoiceConnectionUDPClient {
|
||||
|
||||
}
|
||||
|
||||
module.exports = VoiceConnectionUDPClient;
|
||||
57
src/client/voice/VoiceConnectionWebSocket.js
Normal file
57
src/client/voice/VoiceConnectionWebSocket.js
Normal file
@@ -0,0 +1,57 @@
|
||||
const WebSocket = require('ws');
|
||||
const Constants = require('../../util/Constants');
|
||||
const EventEmitter = require('events').EventEmitter;
|
||||
|
||||
class VoiceConnectionWebSocket extends EventEmitter {
|
||||
constructor(voiceConnection, serverID, token, sessionID, endpoint) {
|
||||
super();
|
||||
this.voiceConnection = voiceConnection;
|
||||
this.token = token;
|
||||
this.sessionID = sessionID;
|
||||
this.serverID = serverID;
|
||||
this.ws = new WebSocket(`wss://${endpoint}`, null, { rejectUnauthorized: false });
|
||||
this.ws.onopen = () => this._onOpen();
|
||||
this.ws.onmessage = e => this._onMessage(e);
|
||||
}
|
||||
|
||||
send(data) {
|
||||
if (this.ws.readyState === WebSocket.OPEN) {
|
||||
this.ws.send(JSON.stringify(data));
|
||||
}
|
||||
}
|
||||
|
||||
_onOpen() {
|
||||
this.send({
|
||||
op: Constants.OPCodes.DISPATCH,
|
||||
d: {
|
||||
server_id: this.serverID,
|
||||
user_id: this.voiceConnection.manager.client.user.id,
|
||||
session_id: this.sessionID,
|
||||
token: this.token,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
_onError(e) {
|
||||
throw e;
|
||||
}
|
||||
|
||||
_onMessage(event) {
|
||||
let packet;
|
||||
try {
|
||||
packet = JSON.parse(event.data);
|
||||
} catch (error) {
|
||||
return this._onError(error);
|
||||
}
|
||||
|
||||
switch (packet.op) {
|
||||
case 2:
|
||||
this.emit('ready-for-udp', packet.d);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = VoiceConnectionWebSocket;
|
||||
Reference in New Issue
Block a user