add closeWS function

This commit is contained in:
ITOH
2021-04-15 16:46:44 +02:00
parent 2c97964f0f
commit 8c52abce7f
6 changed files with 18 additions and 12 deletions
+6
View File
@@ -0,0 +1,6 @@
/** Use this function to close a ws connection properly */
export function closeWS(ws: WebSocket, code?: number, reason?: string) {
if (ws.readyState !== WebSocket.OPEN) return;
ws.close(code, reason);
}
+2 -1
View File
@@ -1,5 +1,6 @@
import { DiscordGatewayOpcodes } from "../types/codes/gateway_opcodes.ts";
import { delay } from "../util/utils.ts";
import { closeWS } from "./close_ws.ts";
import { identify } from "./identify.ts";
import { ws } from "./ws.ts";
@@ -43,7 +44,7 @@ export async function heartbeat(shardId: number, interval: number) {
}
if (!currentShard.heartbeat.acknowledged) {
currentShard.ws.close(3065, "Did not receive an ACK in time.");
closeWS(currentShard.ws, 3066, "Did not receive an ACK in time.");
return identify(shardId, ws.maxShards);
}
+2 -3
View File
@@ -1,4 +1,5 @@
import { DiscordGatewayOpcodes } from "../types/codes/gateway_opcodes.ts";
import { closeWS } from "./close_ws.ts";
import { ws } from "./ws.ts";
export async function identify(shardId: number, maxShards: number) {
@@ -7,9 +8,7 @@ export async function identify(shardId: number, maxShards: number) {
// Need to clear the old heartbeat interval
const oldShard = ws.shards.get(shardId);
if (oldShard) {
if (oldShard.ws.readyState === WebSocket.OPEN) {
oldShard.ws.close(3065, "Reidentifying closure of old shard");
}
closeWS(oldShard.ws, 3065, "Reidentifying closure of old shard");
clearInterval(oldShard.heartbeat.intervalId);
}
+3 -5
View File
@@ -1,4 +1,5 @@
import { DiscordGatewayOpcodes } from "../types/codes/gateway_opcodes.ts";
import { closeWS } from "./close_ws.ts";
import { identify } from "./identify.ts";
import { ws } from "./ws.ts";
@@ -13,11 +14,8 @@ export async function resume(shardId: number) {
// CREATE A SHARD
const socket = await ws.createShard(shardId);
// ONLY CLOSE IF SHARD SOCKET IS STILL CONNECTED
if (oldShard.ws.readyState === WebSocket.OPEN) {
// HOW TO CLOSE OLD SHARD SOCKET!!!
oldShard.ws.close(3065, "Resuming the shard, closing old shard.");
}
// HOW TO CLOSE OLD SHARD SOCKET!!!
closeWS(oldShard.ws, 3064, "Resuming the shard, closing old shard.");
// STOP OLD HEARTBEAT
clearInterval(oldShard.heartbeat.intervalId);
+3 -2
View File
@@ -1,3 +1,4 @@
import { closeWS } from "./close_ws.ts";
import { ws } from "./ws.ts";
/** Allows users to hook in and change to communicate to different clusters across different servers or anything they like. For example using redis pubsub to talk to other servers. */
@@ -10,7 +11,7 @@ export async function tellClusterToIdentify(
const oldShard = ws.shards.get(shardId);
await ws.identify(shardId, ws.maxShards);
if (oldShard?.ws.readyState === WebSocket.OPEN) {
oldShard.ws.close(3065, "Resharded!");
if (oldShard) {
closeWS(oldShard.ws, 3063, "Resharded!");
}
}
+2 -1
View File
@@ -1,4 +1,5 @@
import { delay } from "../../src/util/utils.ts";
import { closeWS } from "../../src/ws/close_ws.ts";
import { ws } from "../../src/ws/ws.ts";
import { defaultTestOptions } from "./start_bot.ts";
@@ -8,7 +9,7 @@ Deno.test({
async fn() {
ws.shards.forEach((shard) => {
clearInterval(shard.heartbeat.intervalId);
shard.ws.close(3064, "Discordeno Testing Finished! Do Not RESUME!");
closeWS(shard.ws, 3061, "Discordeno Testing Finished! Do Not RESUME!");
});
await delay(3000);