Skip to content

Commit

Permalink
🚑️ fix: (try to fix) disconnect, reconnect process
Browse files Browse the repository at this point in the history
  • Loading branch information
Helloyunho committed Nov 9, 2023
1 parent 52b75c2 commit be4db3c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 10 deletions.
29 changes: 22 additions & 7 deletions core/src/gateway/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export class Gateway extends EventEmitter<GatewayEvents> {
private sequence: number | null = null;
private sessionID: string | null = null;
private connected = false;
private retryCount = 0;
private connectionError = false;

constructor(
token: string,
Expand All @@ -66,7 +68,7 @@ export class Gateway extends EventEmitter<GatewayEvents> {
}

connect() {
return new Promise<void>((resolve, reject) => {
return new Promise<void>((resolve) => {
this.ws = new WebSocket(
`${DISCORD_GATEWAY_BASE}/?v=${DISCORD_API_VERSION}&encoding=json`,
);
Expand All @@ -77,27 +79,26 @@ export class Gateway extends EventEmitter<GatewayEvents> {
};
this.ws.onmessage = this.onmessage.bind(this);
this.ws.onclose = this.onclose.bind(this);
this.ws.onerror = (e) => {
reject(e);
};
this.ws.onerror = this.onerror.bind(this);
this.ws.binaryType = "arraybuffer";
});
}

disconnect(code?: GatewayCloseCode) {
disconnect(code?: GatewayCloseCode, reason?: string) {
return new Promise<void>((resolve) => {
this.ws.onclose = (e) => {
this.onclose.bind(this)(e);
resolve();
};
this.ws.close(code);
this.ws.close(code, reason ?? "");
this.initVariables();
this.connected = false;
});
}

private onopen() {
this.connected = true;
this.retryCount = 0;
this.emit("CONNECTED");
}

Expand Down Expand Up @@ -184,6 +185,15 @@ export class Gateway extends EventEmitter<GatewayEvents> {
this.emit("CLOSED", e.code, true, false);
this.reconnect();
break;
case 0:
this.emit("CLOSED", e.code, true, false);
if (this.retryCount < 5) {
setTimeout(() => {
this.retryCount++;
this.reconnect(!this.connectionError);
}, 500);
}
break;
case GatewayCloseCode.INVALID_SHARD:
case GatewayCloseCode.SHARDING_REQUIRED:
case GatewayCloseCode.INVALID_VERSION:
Expand All @@ -196,10 +206,15 @@ export class Gateway extends EventEmitter<GatewayEvents> {
}
}

private onerror(ev: Event | ErrorEvent) {
this.emit("ERROR", ev);
this.connectionError = true;
}

reconnect(resume = false, code?: number) {
this.resume = resume;
if (this.connected) {
this.disconnect(code);
this.disconnect(code, "reconnect");
}
this.connect();
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/gateway/sharded.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GetGatewayBotPayload } from "../../../types/mod.ts";
import { GatewayCloseCode, GetGatewayBotPayload } from "../../../types/mod.ts";
import { EventEmitter } from "../../deps.ts";
import {
GatewayEvents,
Expand Down Expand Up @@ -98,7 +98,7 @@ export class ShardedGateway extends EventEmitter<ShardedGatewayEvents> {

async destroy(shardID: number) {
if (!this.shards[shardID]) return;
await this.shards[shardID].disconnect();
await this.shards[shardID].disconnect(GatewayCloseCode.NORMAL, "destroy");
delete this.shards[shardID];
}

Expand Down
1 change: 1 addition & 0 deletions core/types/gateway/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export type GatewayEvents = {
RAW: [GatewayPayload];
CLOSED: [number, boolean, boolean];
CONNECTED: [];
ERROR: [Event | ErrorEvent];
};
export type ShardedGatewayEvents = {
[K in keyof GatewayEvents]: [number, ...GatewayEvents[K]];
Expand Down
1 change: 1 addition & 0 deletions types/src/gateways/closeCode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export enum GatewayCloseCode {
NORMAL = 1000,
UNKNOWN_ERROR = 4000,
UNKNOWN_OPCODE = 4001,
DECODE_ERROR = 4002,
Expand Down
2 changes: 1 addition & 1 deletion types/src/guilds/member.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface GuildMemberPayload {
mute: boolean;
flags: number;
pending?: boolean;
permissions: string;
permissions?: string;
communication_disabled_until?: string | null;
}

Expand Down

0 comments on commit be4db3c

Please sign in to comment.