change: move closeWS & sendShardMessage to ws object

This commit is contained in:
ITOH
2021-05-06 15:28:42 +02:00
parent c6a44c9665
commit fb8035c4ef
8 changed files with 18 additions and 21 deletions

View File

@@ -5,7 +5,6 @@ import { Errors } from "../../types/discordeno/errors.ts";
import { DiscordGatewayIntents } from "../../types/gateway/gateway_intents.ts";
import type { RequestGuildMembers } from "../../types/members/request_guild_members.ts";
import { Collection } from "../../util/collection.ts";
import { sendShardMessage } from "../../ws/send_shard_message.ts";
import { ws } from "../../ws/ws.ts";
/**
@@ -37,7 +36,7 @@ export function fetchMembers(
const nonce = `${guildId}-${Date.now()}`;
cache.fetchAllMembersProcessingRequests.set(nonce, resolve);
sendShardMessage(shardId, {
ws.sendShardMessage(shardId, {
op: DiscordGatewayOpcodes.RequestGuildMembers,
d: {
guild_id: guildId,

View File

@@ -1,7 +1,6 @@
import { eventHandlers } from "../../bot.ts";
import { DiscordGatewayOpcodes } from "../../types/codes/gateway_opcodes.ts";
import type { StatusUpdate } from "../../types/gateway/status_update.ts";
import { sendShardMessage } from "../../ws/send_shard_message.ts";
import { ws } from "../../ws/ws.ts";
export function editBotStatus(data: Omit<StatusUpdate, "afk" | "since">) {
@@ -11,7 +10,7 @@ export function editBotStatus(data: Omit<StatusUpdate, "afk" | "since">) {
`Running forEach loop in editBotStatus function.`,
);
sendShardMessage(shard, {
ws.sendShardMessage(shard, {
op: DiscordGatewayOpcodes.StatusUpdate,
d: {
since: null,

View File

@@ -8,7 +8,6 @@ import { camelize, delay } from "../util/utils.ts";
import { decompressWith } from "./deps.ts";
import { identify } from "./identify.ts";
import { resume } from "./resume.ts";
import { sendShardMessage } from "./send_shard_message.ts";
import { ws } from "./ws.ts";
/** Handler for handling every message event from websocket. */
@@ -39,7 +38,7 @@ export async function handleOnMessage(message: any, shardId: number) {
shard.heartbeat.lastSentAt = Date.now();
// Discord randomly sends this requiring an immediate heartbeat back
sendShardMessage(shard, {
ws.sendShardMessage(shard, {
op: DiscordGatewayOpcodes.Heartbeat,
d: shard?.previousSequenceNumber,
}, true);

View File

@@ -1,6 +1,5 @@
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";
@@ -45,7 +44,7 @@ export async function heartbeat(shardId: number, interval: number) {
}
if (!currentShard.heartbeat.acknowledged) {
closeWS(currentShard.ws, 3066, "Did not receive an ACK in time.");
ws.closeWS(currentShard.ws, 3066, "Did not receive an ACK in time.");
return identify(shardId, ws.maxShards);
}

View File

@@ -1,6 +1,4 @@
import { DiscordGatewayOpcodes } from "../types/codes/gateway_opcodes.ts";
import { closeWS } from "./close_ws.ts";
import { sendShardMessage } from "./send_shard_message.ts";
import { ws } from "./ws.ts";
export async function identify(shardId: number, maxShards: number) {
@@ -9,7 +7,7 @@ export async function identify(shardId: number, maxShards: number) {
// Need to clear the old heartbeat interval
const oldShard = ws.shards.get(shardId);
if (oldShard) {
closeWS(oldShard.ws, 3065, "Reidentifying closure of old shard");
ws.closeWS(oldShard.ws, 3065, "Reidentifying closure of old shard");
clearInterval(oldShard.heartbeat.intervalId);
}
@@ -41,7 +39,7 @@ export async function identify(shardId: number, maxShards: number) {
});
socket.onopen = () => {
sendShardMessage(shardId, {
ws.sendShardMessage(shardId, {
op: DiscordGatewayOpcodes.Identify,
d: { ...ws.identifyPayload, shard: [shardId, maxShards] },
}, true);

View File

@@ -1,6 +1,4 @@
import { DiscordGatewayOpcodes } from "../types/codes/gateway_opcodes.ts";
import { closeWS } from "./close_ws.ts";
import { sendShardMessage } from "./send_shard_message.ts";
import { ws } from "./ws.ts";
export async function resume(shardId: number) {
@@ -12,7 +10,7 @@ export async function resume(shardId: number) {
if (oldShard) {
// HOW TO CLOSE OLD SHARD SOCKET!!!
closeWS(oldShard.ws, 3064, "Resuming the shard, closing old shard.");
ws.closeWS(oldShard.ws, 3064, "Resuming the shard, closing old shard.");
// STOP OLD HEARTBEAT
clearInterval(oldShard.heartbeat.intervalId);
}
@@ -48,7 +46,7 @@ export async function resume(shardId: number) {
// Resume on open
socket.onopen = () => {
sendShardMessage(shardId, {
ws.sendShardMessage(shardId, {
op: DiscordGatewayOpcodes.Resume,
d: {
token: ws.identifyPayload.token,

View File

@@ -1,6 +1,7 @@
import { DiscordGatewayOpcodes } from "../types/codes/gateway_opcodes.ts";
import { Collection } from "../util/collection.ts";
import { cleanupLoadingShards } from "./cleanup_loading_shards.ts";
import { closeWS } from "./close_ws.ts";
import { createShard } from "./create_shard.ts";
import { log } from "./events.ts";
import { handleDiscordPayload } from "./handle_discord_payload.ts";
@@ -9,6 +10,7 @@ import { heartbeat } from "./heartbeat.ts";
import { identify } from "./identify.ts";
import { processQueue } from "./process_queue.ts";
import { resharder } from "./resharder.ts";
import { sendShardMessage } from "./send_shard_message.ts";
import { spawnShards } from "./spawn_shards.ts";
import { startGateway } from "./start_gateway.ts";
import { tellClusterToIdentify } from "./tell_cluster_to_identify.ts";
@@ -92,7 +94,7 @@ export const ws = {
spawnShards,
/** Create the websocket and adds the proper handlers to the websocket. */
createShard,
/** Begins identification of the shard to discord */
/** Begins identification of the shard to discord. */
identify,
/** Begins heartbeating of the shard to keep it alive */
heartbeat,
@@ -106,10 +108,14 @@ export const ws = {
resharder,
/** Cleanups loading shards that were unable to load. */
cleanupLoadingShards,
/** Handles the message events from websocket */
/** Handles the message events from websocket. */
handleOnMessage,
/** Handles processing queue of requests send to this shard */
/** Handles processing queue of requests send to this shard. */
processQueue,
/** Closes shard WebSocket connection properly. */
closeWS,
/** Properly adds a message to the shards queue */
sendShardMessage,
};
export interface DiscordenoShard {

View File

@@ -1,5 +1,4 @@
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";
@@ -9,7 +8,7 @@ Deno.test({
async fn() {
ws.shards.forEach((shard) => {
clearInterval(shard.heartbeat.intervalId);
closeWS(shard.ws, 3061, "Discordeno Testing Finished! Do Not RESUME!");
ws.closeWS(shard.ws, 3061, "Discordeno Testing Finished! Do Not RESUME!");
});
await delay(3000);