fix(shard): fix wsl gateway

related to
https://github.com/oven-sh/bun/issues/521
This commit is contained in:
Dave Caruso 2022-07-12 02:35:32 -04:00
parent 6363045eb7
commit cf2c5f3e8f

View File

@ -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<void> {
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<void> {
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)) {