add resume_gateway_url to DiscordReady (#2415)

* add resume_gateway_url to DiscordReady

* gateway: resumeGatewayUrl

* gateway: implement resumeGatewayUrl handle for shard

* formatting

* gateway: fix websocket url

Co-authored-by: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com>
This commit is contained in:
LTS20050703
2022-09-08 12:47:01 -04:00
committed by GitHub
co-authored by Skillz4Killz
parent a5d960b5f1
commit 297d5dbb28
5 changed files with 32 additions and 19 deletions
+1
View File
@@ -197,6 +197,7 @@ export async function tellWorkerToPrepare(resharder: Resharder, shardId: number)
if (messageData.t === "READY") {
const payload = messageData.d as DiscordReady;
shard.resumeGatewayUrl = payload.resume_gateway_url;
await resharder.markNewGuildShardId(payload.guilds.map((g) => g.id), shardId);
}
},
+10 -2
View File
@@ -8,8 +8,16 @@ export async function connect(shard: Shard): Promise<void> {
}
shard.events.connecting?.(shard);
// Explicitly setting the encoding to json, since we do not support ETF.
const socket = new WebSocket(`${shard.gatewayConfig.url}/?v=${shard.gatewayConfig.version}&encoding=json`);
const socket = new WebSocket(
`${
shard.gatewayConfig.url === "wss://gateway.discord.gg"
? shard.state === ShardState.Resuming
? shard.resumeGatewayUrl
: `${shard.gatewayConfig.url}/?v=${shard.gatewayConfig.version}&encoding=json`
: shard.gatewayConfig.url
}`,
);
shard.socket = socket;
// TODO: proper event handling
+17 -17
View File
@@ -1,5 +1,20 @@
import { identify } from "./identify.ts";
import { StatusUpdate } from "../../helpers/misc/editShardStatus.ts";
import { DiscordGatewayPayload } from "../../types/discord.ts";
import { PickPartial } from "../../types/shared.ts";
import { createLeakyBucket, LeakyBucket } from "../../util/bucket.ts";
import { API_VERSION } from "../../util/constants.ts";
import { calculateSafeRequests } from "./calculateSafeRequests.ts";
import { close } from "./close.ts";
import { connect } from "./connect.ts";
import { handleClose } from "./handleClose.ts";
import { handleMessage } from "./handleMessage.ts";
import { identify } from "./identify.ts";
import { isOpen } from "./isOpen.ts";
import { resume } from "./resume.ts";
import { send } from "./send.ts";
import { shutdown } from "./shutdown.ts";
import { startHeartbeating } from "./startHeartbeating.ts";
import { stopHeartbeating } from "./stopHeartbeating.ts";
import {
DEFAULT_HEARTBEAT_INTERVAL,
GATEWAY_RATE_LIMIT_RESET_INTERVAL,
@@ -8,25 +23,9 @@ import {
ShardEvents,
ShardGatewayConfig,
ShardHeart,
ShardSocketCloseCodes,
ShardSocketRequest,
ShardState,
} from "./types.ts";
import { StatusUpdate } from "../../helpers/misc/editShardStatus.ts";
import { startHeartbeating } from "./startHeartbeating.ts";
import { stopHeartbeating } from "./stopHeartbeating.ts";
import { resume } from "./resume.ts";
import { createLeakyBucket, LeakyBucket } from "../../util/bucket.ts";
import { calculateSafeRequests } from "./calculateSafeRequests.ts";
import { send } from "./send.ts";
import { handleClose } from "./handleClose.ts";
import { connect } from "./connect.ts";
import { close } from "./close.ts";
import { shutdown } from "./shutdown.ts";
import { isOpen } from "./isOpen.ts";
import { DiscordGatewayPayload } from "../../types/discord.ts";
import { GatewayIntents, PickPartial } from "../../types/shared.ts";
import { API_VERSION } from "../../util/constants.ts";
// TODO: debug
// TODO: function overwrite
@@ -91,6 +90,7 @@ export function createShard(
state: ShardState.Offline,
/** The total amount of shards which are used to communicate with Discord. */
totalShards: options.totalShards,
resumeGatewayUrl: "",
// ----------
// METHODS
+2
View File
@@ -130,6 +130,8 @@ export async function handleMessage(shard: Shard, message: MessageEvent<any>): P
else if (messageData.t === "READY") {
const payload = messageData.d as DiscordReady;
shard.resumeGatewayUrl = payload.resume_gateway_url;
shard.sessionId = payload.session_id;
shard.state = ShardState.Connected;
+2
View File
@@ -2173,6 +2173,8 @@ export interface DiscordReady {
guilds: DiscordUnavailableGuild[];
/** Used for resuming connections */
session_id: string;
/** Gateway url for resuming connections */
resume_gateway_url: string;
/** The shard information associated with this session, if sent when identifying */
shard?: [number, number];
/** Contains id and flags */