Skip to content

Commit

Permalink
fix(gateway): Use Deno's WebSocket (#3030)
Browse files Browse the repository at this point in the history
* fix(gateway): Use Deno's WebSocket

* invert runtime check
  • Loading branch information
Endy3032 committed Jun 5, 2023
1 parent 169e117 commit dcc121a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
8 changes: 0 additions & 8 deletions packages/bot/src/bot.ts
Expand Up @@ -67,14 +67,6 @@ export function createBot(options: CreateBotOptions): Bot {
// Set up helpers below.
helpers: {} as BotHelpers,
async start() {
// @ts-expect-error should this work
if (typeof Deno !== 'undefined') {
// @ts-expect-error should this work
const katsura = await import('https://x.nest.land/katsura@1.3.9/src/discordenoFixes/gatewaySocket.ts')

await katsura(bot.gateway)
}

if (!options.gateway?.connection) {
bot.gateway.connection = await bot.rest.getSessionInfo()
}
Expand Down
22 changes: 12 additions & 10 deletions packages/gateway/src/Shard.ts
Expand Up @@ -12,11 +12,13 @@ import type {
import { GatewayCloseEventCodes, GatewayIntents, GatewayOpcodes } from '@discordeno/types'
import { Collection, LeakyBucket, camelize, delay, logger } from '@discordeno/utils'
import { inflateSync } from 'node:zlib'
import WebSocket from 'ws'
import NodeWebSocket from 'ws'
import type { RequestMemberRequest } from './manager.js'
import type { BotStatusUpdate, ShardEvents, ShardGatewayConfig, ShardHeart, ShardSocketRequest, StatusUpdate, UpdateVoiceState } from './types.js'
import { ShardSocketCloseCodes, ShardState } from './types.js'

declare let WebSocket: any

export class DiscordenoShard {
/** The id of the shard */
id: number
Expand All @@ -33,7 +35,7 @@ export class DiscordenoShard {
/** Current session id of the shard if present. */
sessionId?: string
/** This contains the WebSocket connection to Discord, if currently connected. */
socket?: WebSocket
socket?: NodeWebSocket
/** Current internal state of the this. */
state = ShardState.Offline
/** The url provided by discord to use when resuming a connection for this this. */
Expand Down Expand Up @@ -111,7 +113,7 @@ export class DiscordenoShard {

/** Close the socket connection to discord if present. */
close(code: number, reason: string): void {
if (this.socket?.readyState !== WebSocket.OPEN) return
if (this.socket?.readyState !== NodeWebSocket.OPEN) return

this.socket?.close(code, reason)
}
Expand All @@ -129,13 +131,13 @@ export class DiscordenoShard {
url.searchParams.set('v', this.gatewayConfig.version.toString())
url.searchParams.set('encoding', 'json')

const socket = new WebSocket(url.toString())
const socket: NodeWebSocket = process?.versions !== undefined ? new NodeWebSocket(url.toString()) : new WebSocket(url.toString())
this.socket = socket

// TODO: proper event handling
socket.onerror = (event) => console.log({ error: event, shardId: this.id })
socket.onclose = async (event) => await this.handleClose(event)
socket.onmessage = async (message) => await this.handleMessage(message)
socket.onerror = (event: NodeWebSocket.ErrorEvent) => console.log({ error: event, shardId: this.id })
socket.onclose = async (event: NodeWebSocket.CloseEvent) => await this.handleClose(event)
socket.onmessage = async (message: NodeWebSocket.MessageEvent) => await this.handleMessage(message)

return await new Promise((resolve) => {
socket.onopen = () => {
Expand Down Expand Up @@ -204,7 +206,7 @@ export class DiscordenoShard {

/** Check whether the connection to Discord is currently open. */
isOpen(): boolean {
return this.socket?.readyState === WebSocket.OPEN
return this.socket?.readyState === NodeWebSocket.OPEN
}

/** Attempt to resume the previous shards session with the gateway. */
Expand Down Expand Up @@ -282,7 +284,7 @@ export class DiscordenoShard {
}

/** Handle a gateway connection close. */
async handleClose(close: WebSocket.CloseEvent): Promise<void> {
async handleClose(close: NodeWebSocket.CloseEvent): Promise<void> {
// gateway.debug("GW CLOSED", { shardId, payload: event });

this.stopHeartbeating()
Expand Down Expand Up @@ -485,7 +487,7 @@ export class DiscordenoShard {
}

/** Handle an incoming gateway message. */
async handleMessage(message: WebSocket.MessageEvent): Promise<void> {
async handleMessage(message: NodeWebSocket.MessageEvent): Promise<void> {
let preProcessMessage = message.data

// If message compression is enabled,
Expand Down

0 comments on commit dcc121a

Please sign in to comment.