mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-16 11:28:15 +00:00
add closeWS function
This commit is contained in:
@@ -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
@@ -1,5 +1,6 @@
|
|||||||
import { DiscordGatewayOpcodes } from "../types/codes/gateway_opcodes.ts";
|
import { DiscordGatewayOpcodes } from "../types/codes/gateway_opcodes.ts";
|
||||||
import { delay } from "../util/utils.ts";
|
import { delay } from "../util/utils.ts";
|
||||||
|
import { closeWS } from "./close_ws.ts";
|
||||||
import { identify } from "./identify.ts";
|
import { identify } from "./identify.ts";
|
||||||
import { ws } from "./ws.ts";
|
import { ws } from "./ws.ts";
|
||||||
|
|
||||||
@@ -43,7 +44,7 @@ export async function heartbeat(shardId: number, interval: number) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!currentShard.heartbeat.acknowledged) {
|
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);
|
return identify(shardId, ws.maxShards);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-3
@@ -1,4 +1,5 @@
|
|||||||
import { DiscordGatewayOpcodes } from "../types/codes/gateway_opcodes.ts";
|
import { DiscordGatewayOpcodes } from "../types/codes/gateway_opcodes.ts";
|
||||||
|
import { closeWS } from "./close_ws.ts";
|
||||||
import { ws } from "./ws.ts";
|
import { ws } from "./ws.ts";
|
||||||
|
|
||||||
export async function identify(shardId: number, maxShards: number) {
|
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
|
// Need to clear the old heartbeat interval
|
||||||
const oldShard = ws.shards.get(shardId);
|
const oldShard = ws.shards.get(shardId);
|
||||||
if (oldShard) {
|
if (oldShard) {
|
||||||
if (oldShard.ws.readyState === WebSocket.OPEN) {
|
closeWS(oldShard.ws, 3065, "Reidentifying closure of old shard");
|
||||||
oldShard.ws.close(3065, "Reidentifying closure of old shard");
|
|
||||||
}
|
|
||||||
clearInterval(oldShard.heartbeat.intervalId);
|
clearInterval(oldShard.heartbeat.intervalId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-4
@@ -1,4 +1,5 @@
|
|||||||
import { DiscordGatewayOpcodes } from "../types/codes/gateway_opcodes.ts";
|
import { DiscordGatewayOpcodes } from "../types/codes/gateway_opcodes.ts";
|
||||||
|
import { closeWS } from "./close_ws.ts";
|
||||||
import { identify } from "./identify.ts";
|
import { identify } from "./identify.ts";
|
||||||
import { ws } from "./ws.ts";
|
import { ws } from "./ws.ts";
|
||||||
|
|
||||||
@@ -13,11 +14,8 @@ export async function resume(shardId: number) {
|
|||||||
// CREATE A SHARD
|
// CREATE A SHARD
|
||||||
const socket = await ws.createShard(shardId);
|
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!!!
|
// HOW TO CLOSE OLD SHARD SOCKET!!!
|
||||||
oldShard.ws.close(3065, "Resuming the shard, closing old shard.");
|
closeWS(oldShard.ws, 3064, "Resuming the shard, closing old shard.");
|
||||||
}
|
|
||||||
// STOP OLD HEARTBEAT
|
// STOP OLD HEARTBEAT
|
||||||
clearInterval(oldShard.heartbeat.intervalId);
|
clearInterval(oldShard.heartbeat.intervalId);
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { closeWS } from "./close_ws.ts";
|
||||||
import { ws } from "./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. */
|
/** 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);
|
const oldShard = ws.shards.get(shardId);
|
||||||
await ws.identify(shardId, ws.maxShards);
|
await ws.identify(shardId, ws.maxShards);
|
||||||
|
|
||||||
if (oldShard?.ws.readyState === WebSocket.OPEN) {
|
if (oldShard) {
|
||||||
oldShard.ws.close(3065, "Resharded!");
|
closeWS(oldShard.ws, 3063, "Resharded!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { delay } from "../../src/util/utils.ts";
|
import { delay } from "../../src/util/utils.ts";
|
||||||
|
import { closeWS } from "../../src/ws/close_ws.ts";
|
||||||
import { ws } from "../../src/ws/ws.ts";
|
import { ws } from "../../src/ws/ws.ts";
|
||||||
import { defaultTestOptions } from "./start_bot.ts";
|
import { defaultTestOptions } from "./start_bot.ts";
|
||||||
|
|
||||||
@@ -8,7 +9,7 @@ Deno.test({
|
|||||||
async fn() {
|
async fn() {
|
||||||
ws.shards.forEach((shard) => {
|
ws.shards.forEach((shard) => {
|
||||||
clearInterval(shard.heartbeat.intervalId);
|
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);
|
await delay(3000);
|
||||||
|
|||||||
Reference in New Issue
Block a user