mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 09:03:29 +01:00
document websocketmanager
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -4,18 +4,51 @@ const zlib = require('zlib');
|
||||
const PacketManager = require('./packets/WebSocketPacketManager');
|
||||
const WebSocketManagerDataStore = require('../../structures/datastore/WebSocketManagerDataStore');
|
||||
|
||||
/**
|
||||
* The WebSocket Manager of the Client
|
||||
* @private
|
||||
*/
|
||||
class WebSocketManager {
|
||||
|
||||
constructor(client) {
|
||||
/**
|
||||
* The Client that instantiated this WebSocketManager
|
||||
* @type {Client}
|
||||
*/
|
||||
this.client = client;
|
||||
this.ws = null;
|
||||
/**
|
||||
* A WebSocket Packet manager, it handles all the messages
|
||||
* @type {PacketManager}
|
||||
*/
|
||||
this.packetManager = new PacketManager(this);
|
||||
/**
|
||||
* The WebSocketManager datastore
|
||||
* @type {WebSocketManagerDataStore}
|
||||
*/
|
||||
this.store = new WebSocketManagerDataStore();
|
||||
/**
|
||||
* The status of the WebSocketManager, a type of Constants.Status. It defaults to IDLE.
|
||||
* @type {Number}
|
||||
*/
|
||||
this.status = Constants.Status.IDLE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Connects the client to a given gateway
|
||||
* @param {String} gateway the gateway to connect to
|
||||
* @returns {null}
|
||||
*/
|
||||
connect(gateway) {
|
||||
/**
|
||||
* The status of the Client's connection, a type of Constants.Status
|
||||
* @type {Number}
|
||||
*/
|
||||
this.status = Constants.Status.CONNECTING;
|
||||
/**
|
||||
* The WebSocket connection to the gateway
|
||||
* @type {?WebSocket}
|
||||
*/
|
||||
this.ws = new WebSocket(gateway);
|
||||
this.ws.onopen = () => this.eventOpen();
|
||||
this.ws.onclose = () => this.eventClose();
|
||||
@@ -23,12 +56,21 @@ class WebSocketManager {
|
||||
this.ws.onerror = (e) => this.eventError(e);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a packet to the gateway
|
||||
* @param {Object} packet An object that can be JSON stringified
|
||||
* @returns {null}
|
||||
*/
|
||||
send(data) {
|
||||
if (this.ws.readyState === WebSocket.OPEN) {
|
||||
this.ws.send(JSON.stringify(data));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Run whenever the gateway connections opens up
|
||||
* @returns {null}
|
||||
*/
|
||||
eventOpen() {
|
||||
if (this.reconnecting) {
|
||||
this._sendResume();
|
||||
@@ -37,6 +79,10 @@ class WebSocketManager {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a gatway resume packet, in cases of unexpected disconnections.
|
||||
* @returns {null}
|
||||
*/
|
||||
_sendResume() {
|
||||
const payload = {
|
||||
token: this.client.store.token,
|
||||
@@ -50,6 +96,10 @@ class WebSocketManager {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a new identification packet, in cases of new connections or failed reconnections.
|
||||
* @returns {null}
|
||||
*/
|
||||
_sendNewIdentify() {
|
||||
this.reconnecting = false;
|
||||
const payload = this.client.options.ws;
|
||||
@@ -61,12 +111,22 @@ class WebSocketManager {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Run whenever the connection to the gateway is closed, it will try to reconnect the client.
|
||||
* @returns {null}
|
||||
*/
|
||||
eventClose() {
|
||||
if (!this.reconnecting) {
|
||||
this.tryReconnect();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Run whenever a message is received from the WebSocket. Returns `true` if the message
|
||||
* was handled properly.
|
||||
* @param {Object} data the received websocket data
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
eventMessage($event) {
|
||||
let packet;
|
||||
const event = $event;
|
||||
@@ -87,10 +147,19 @@ class WebSocketManager {
|
||||
return this.packetManager.handle(packet);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run whenever an error occurs with the WebSocket connection. Tries to reconnect
|
||||
* @returns {null}
|
||||
*/
|
||||
eventError() {
|
||||
this.tryReconnect();
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs on new packets before `READY` to see if the Client is ready yet, if it is prepares
|
||||
* the `READY` event.
|
||||
* @returns {null}
|
||||
*/
|
||||
checkIfReady() {
|
||||
if (this.status !== Constants.Status.READY) {
|
||||
let unavailableCount = 0;
|
||||
@@ -106,6 +175,10 @@ class WebSocketManager {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to reconnect the client, changing the status to Constants.Status.RECONNECTING.
|
||||
* @returns {null}
|
||||
*/
|
||||
tryReconnect() {
|
||||
this.status = Constants.Status.RECONNECTING;
|
||||
this.ws.close();
|
||||
|
||||
Reference in New Issue
Block a user