mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-15 02:53:31 +01:00
src/client/websocket/packets/WebSocketPacketManager.js
src/structures/datastore/WebSocketManagerDataStore.js Added "rough" reconnection attempting The following however still needs to be done: 1) stop trying after a certain amount of time 2) increasing timeout between each timeout 3) re-evaluate the current method of how reconnecting is handled internally
This commit is contained in:
@@ -4,6 +4,7 @@ const WebSocket = require('ws');
|
|||||||
const Constants = require('../../util/Constants');
|
const Constants = require('../../util/Constants');
|
||||||
const zlib = require('zlib');
|
const zlib = require('zlib');
|
||||||
const PacketManager = require('./packets/WebSocketPacketManager');
|
const PacketManager = require('./packets/WebSocketPacketManager');
|
||||||
|
const WebSocketManagerDataStore = require('../../structures/datastore/WebSocketManagerDataStore');
|
||||||
|
|
||||||
class WebSocketManager {
|
class WebSocketManager {
|
||||||
|
|
||||||
@@ -12,9 +13,12 @@ class WebSocketManager {
|
|||||||
this.ws = null;
|
this.ws = null;
|
||||||
this.packetManager = new PacketManager(this);
|
this.packetManager = new PacketManager(this);
|
||||||
this.emittedReady = false;
|
this.emittedReady = false;
|
||||||
|
this.store = new WebSocketManagerDataStore();
|
||||||
|
this.reconnecting = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
connect(gateway) {
|
connect(gateway) {
|
||||||
|
this.store.gateway = gateway;
|
||||||
gateway += `/?v=${this.client.options.protocol_version}`;
|
gateway += `/?v=${this.client.options.protocol_version}`;
|
||||||
this.ws = new WebSocket(gateway);
|
this.ws = new WebSocket(gateway);
|
||||||
this.ws.onopen = () => this.EventOpen();
|
this.ws.onopen = () => this.EventOpen();
|
||||||
@@ -24,10 +28,34 @@ class WebSocketManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
send(data) {
|
send(data) {
|
||||||
this.ws.send(JSON.stringify(data));
|
if (this.ws.readyState === WebSocket.OPEN) {
|
||||||
|
this.ws.send(JSON.stringify(data));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
EventOpen() {
|
EventOpen() {
|
||||||
|
if (this.reconnecting) {
|
||||||
|
this._sendResume();
|
||||||
|
} else {
|
||||||
|
this._sendNewIdentify();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_sendResume() {
|
||||||
|
let payload = {
|
||||||
|
token: this.client.store.token,
|
||||||
|
session_id: this.store.sessionID,
|
||||||
|
seq: this.store.sequence,
|
||||||
|
};
|
||||||
|
|
||||||
|
this.send({
|
||||||
|
op: Constants.OPCodes.RESUME,
|
||||||
|
d: payload,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
_sendNewIdentify() {
|
||||||
|
this.reconnecting = false;
|
||||||
let payload = this.client.options.ws;
|
let payload = this.client.options.ws;
|
||||||
payload.token = this.client.store.token;
|
payload.token = this.client.store.token;
|
||||||
|
|
||||||
@@ -38,7 +66,9 @@ class WebSocketManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
EventClose() {
|
EventClose() {
|
||||||
|
if (!this.reconnecting) {
|
||||||
|
this.tryReconnect();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
EventMessage(event) {
|
EventMessage(event) {
|
||||||
@@ -57,7 +87,7 @@ class WebSocketManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
EventError(e) {
|
EventError(e) {
|
||||||
|
this.tryReconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
checkIfReady() {
|
checkIfReady() {
|
||||||
@@ -75,6 +105,15 @@ class WebSocketManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tryReconnect() {
|
||||||
|
this.reconnecting = true;
|
||||||
|
this.ws.close();
|
||||||
|
this.packetManager.handleQueue();
|
||||||
|
this.client.emit(Constants.Events.RECONNECTING);
|
||||||
|
this.emittedReady = false;
|
||||||
|
this.connect(this.store.gateway);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = WebSocketManager;
|
module.exports = WebSocketManager;
|
||||||
|
|||||||
@@ -8,8 +8,6 @@ const BeforeReadyWhitelist = [
|
|||||||
Constants.WSEvents.GUILD_DELETE,
|
Constants.WSEvents.GUILD_DELETE,
|
||||||
];
|
];
|
||||||
|
|
||||||
var amount = 0;
|
|
||||||
|
|
||||||
class WebSocketPacketManager {
|
class WebSocketPacketManager {
|
||||||
|
|
||||||
constructor(websocketManager) {
|
constructor(websocketManager) {
|
||||||
@@ -58,8 +56,32 @@ class WebSocketPacketManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setSequence(s) {
|
||||||
|
if (s && s > this.ws.store.sequence) {
|
||||||
|
this.ws.store.sequence = s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
handle(packet) {
|
handle(packet) {
|
||||||
amount++;
|
|
||||||
|
if (packet.op === Constants.OPCodes.RECONNECT) {
|
||||||
|
this.setSequence(packet.s);
|
||||||
|
this.ws.tryReconnect();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (packet.op === Constants.OPCodes.INVALID_SESSION) {
|
||||||
|
this.ws._sendNewIdentify();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.ws.reconnecting) {
|
||||||
|
this.ws.reconnecting = false;
|
||||||
|
this.ws.checkIfReady();
|
||||||
|
}
|
||||||
|
|
||||||
|
this.setSequence(packet.s);
|
||||||
|
|
||||||
if (!this.ws.emittedReady) {
|
if (!this.ws.emittedReady) {
|
||||||
if (BeforeReadyWhitelist.indexOf(packet.t) === -1) {
|
if (BeforeReadyWhitelist.indexOf(packet.t) === -1) {
|
||||||
this.queue.push(packet);
|
this.queue.push(packet);
|
||||||
|
|||||||
@@ -28,6 +28,8 @@ class ReadyHandler extends AbstractHandler {
|
|||||||
client.store.NewChannel(privateDM);
|
client.store.NewChannel(privateDM);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.packetManager.ws.store.sessionID = data.session_id;
|
||||||
|
|
||||||
this.packetManager.ws.checkIfReady();
|
this.packetManager.ws.checkIfReady();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
14
src/structures/datastore/WebSocketManagerDataStore.js
Normal file
14
src/structures/datastore/WebSocketManagerDataStore.js
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const AbstractDataStore = require('./AbstractDataStore');
|
||||||
|
|
||||||
|
class WebSocketManagerDataStore extends AbstractDataStore{
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
this.sessionID = null;
|
||||||
|
this.sequence = -1;
|
||||||
|
this.gateway = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = WebSocketManagerDataStore;
|
||||||
@@ -4,9 +4,10 @@ const Discord = require('../');
|
|||||||
|
|
||||||
let client = new Discord.Client();
|
let client = new Discord.Client();
|
||||||
|
|
||||||
client.login(require('./auth.json').token).then(token => console.log('ready!')).catch(console.log);
|
client.login(require('./auth.json').token).then(token => console.log('logged in with token ' + token)).catch(console.log);
|
||||||
|
|
||||||
client.on('ready', () => {
|
client.on('ready', () => {
|
||||||
|
console.log('ready!');
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on('guildCreate', (guild) => {
|
client.on('guildCreate', (guild) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user