From cf2c5f3e8fd66410cefe697e4a59286b6cabd766 Mon Sep 17 00:00:00 2001 From: Dave Caruso Date: Tue, 12 Jul 2022 02:35:32 -0400 Subject: [PATCH] fix(shard): fix wsl gateway related to https://github.com/oven-sh/bun/issues/521 --- packages/discordeno/gateway/shard/connect.ts | 22 +++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/discordeno/gateway/shard/connect.ts b/packages/discordeno/gateway/shard/connect.ts index 2671f82..adf0fbe 100644 --- a/packages/discordeno/gateway/shard/connect.ts +++ b/packages/discordeno/gateway/shard/connect.ts @@ -1,6 +1,11 @@ import { Shard, ShardState } from "./types.ts"; +// TODO: Remove code marked WSL GATEWAY PATCH once a bug in bun is fixed: +// `https://github.com/Jarred-Sumner/bun/issues/521` + export async function connect(shard: Shard): Promise { + let gotHello = false; + // Only set the shard to `Connecting` state, // if the connection request does not come from an identify or resume action. if (![ShardState.Identifying, ShardState.Resuming].includes(shard.state)) { @@ -17,10 +22,25 @@ export async function connect(shard: Shard): Promise { socket.onclose = (event) => shard.handleClose(event); - socket.onmessage = (message) => shard.handleMessage(message); + socket.onmessage = (message) => { + // START WSL GATEWAY PATCH + gotHello = true; + // END WSL GATEWAY PATCH + shard.handleMessage(message); + } return new Promise((resolve) => { socket.onopen = () => { + // START WSL GATEWAY PATCH + setTimeout(() => { + if (!gotHello) { + shard.handleMessage({ + data: JSON.stringify({ t: null, s: null, op: 10, d: { heartbeat_interval: 41250 } }), + } as any); + } + }, 250); + // END WSL GATEWAY PATCH + // Only set the shard to `Unidentified` state, // if the connection request does not come from an identify or resume action. if (![ShardState.Identifying, ShardState.Resuming].includes(shard.state)) {