mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-19 13:03:31 +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 PacketManager = require('./packets/WebSocketPacketManager');
|
||||||
const WebSocketManagerDataStore = require('../../structures/datastore/WebSocketManagerDataStore');
|
const WebSocketManagerDataStore = require('../../structures/datastore/WebSocketManagerDataStore');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The WebSocket Manager of the Client
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
class WebSocketManager {
|
class WebSocketManager {
|
||||||
|
|
||||||
constructor(client) {
|
constructor(client) {
|
||||||
|
/**
|
||||||
|
* The Client that instantiated this WebSocketManager
|
||||||
|
* @type {Client}
|
||||||
|
*/
|
||||||
this.client = client;
|
this.client = client;
|
||||||
this.ws = null;
|
this.ws = null;
|
||||||
|
/**
|
||||||
|
* A WebSocket Packet manager, it handles all the messages
|
||||||
|
* @type {PacketManager}
|
||||||
|
*/
|
||||||
this.packetManager = new PacketManager(this);
|
this.packetManager = new PacketManager(this);
|
||||||
|
/**
|
||||||
|
* The WebSocketManager datastore
|
||||||
|
* @type {WebSocketManagerDataStore}
|
||||||
|
*/
|
||||||
this.store = new 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;
|
this.status = Constants.Status.IDLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connects the client to a given gateway
|
||||||
|
* @param {String} gateway the gateway to connect to
|
||||||
|
* @returns {null}
|
||||||
|
*/
|
||||||
connect(gateway) {
|
connect(gateway) {
|
||||||
|
/**
|
||||||
|
* The status of the Client's connection, a type of Constants.Status
|
||||||
|
* @type {Number}
|
||||||
|
*/
|
||||||
this.status = Constants.Status.CONNECTING;
|
this.status = Constants.Status.CONNECTING;
|
||||||
|
/**
|
||||||
|
* The WebSocket connection to the gateway
|
||||||
|
* @type {?WebSocket}
|
||||||
|
*/
|
||||||
this.ws = new WebSocket(gateway);
|
this.ws = new WebSocket(gateway);
|
||||||
this.ws.onopen = () => this.eventOpen();
|
this.ws.onopen = () => this.eventOpen();
|
||||||
this.ws.onclose = () => this.eventClose();
|
this.ws.onclose = () => this.eventClose();
|
||||||
@@ -23,12 +56,21 @@ class WebSocketManager {
|
|||||||
this.ws.onerror = (e) => this.eventError(e);
|
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) {
|
send(data) {
|
||||||
if (this.ws.readyState === WebSocket.OPEN) {
|
if (this.ws.readyState === WebSocket.OPEN) {
|
||||||
this.ws.send(JSON.stringify(data));
|
this.ws.send(JSON.stringify(data));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run whenever the gateway connections opens up
|
||||||
|
* @returns {null}
|
||||||
|
*/
|
||||||
eventOpen() {
|
eventOpen() {
|
||||||
if (this.reconnecting) {
|
if (this.reconnecting) {
|
||||||
this._sendResume();
|
this._sendResume();
|
||||||
@@ -37,6 +79,10 @@ class WebSocketManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends a gatway resume packet, in cases of unexpected disconnections.
|
||||||
|
* @returns {null}
|
||||||
|
*/
|
||||||
_sendResume() {
|
_sendResume() {
|
||||||
const payload = {
|
const payload = {
|
||||||
token: this.client.store.token,
|
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() {
|
_sendNewIdentify() {
|
||||||
this.reconnecting = false;
|
this.reconnecting = false;
|
||||||
const payload = this.client.options.ws;
|
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() {
|
eventClose() {
|
||||||
if (!this.reconnecting) {
|
if (!this.reconnecting) {
|
||||||
this.tryReconnect();
|
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) {
|
eventMessage($event) {
|
||||||
let packet;
|
let packet;
|
||||||
const event = $event;
|
const event = $event;
|
||||||
@@ -87,10 +147,19 @@ class WebSocketManager {
|
|||||||
return this.packetManager.handle(packet);
|
return this.packetManager.handle(packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run whenever an error occurs with the WebSocket connection. Tries to reconnect
|
||||||
|
* @returns {null}
|
||||||
|
*/
|
||||||
eventError() {
|
eventError() {
|
||||||
this.tryReconnect();
|
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() {
|
checkIfReady() {
|
||||||
if (this.status !== Constants.Status.READY) {
|
if (this.status !== Constants.Status.READY) {
|
||||||
let unavailableCount = 0;
|
let unavailableCount = 0;
|
||||||
@@ -106,6 +175,10 @@ class WebSocketManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tries to reconnect the client, changing the status to Constants.Status.RECONNECTING.
|
||||||
|
* @returns {null}
|
||||||
|
*/
|
||||||
tryReconnect() {
|
tryReconnect() {
|
||||||
this.status = Constants.Status.RECONNECTING;
|
this.status = Constants.Status.RECONNECTING;
|
||||||
this.ws.close();
|
this.ws.close();
|
||||||
|
|||||||
Reference in New Issue
Block a user