From 0a153379c87ebbee8c7d7b3015fe4c1b77f379ca Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Fri, 1 Sep 2023 06:19:56 -0400 Subject: [PATCH] fix: add support for browser WS by checking if we are in node --- src/transports/webSocket.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/transports/webSocket.ts b/src/transports/webSocket.ts index f245aa2..a22f755 100644 --- a/src/transports/webSocket.ts +++ b/src/transports/webSocket.ts @@ -1,8 +1,8 @@ import { Logger, Peer } from "../types.js"; -import NodeId from "../nodeId.js"; import { URL } from "url"; -import { WebSocket } from "ws"; +import * as WS from "ws"; import { BasePeer } from "#transports/base.js"; +import isNode from "detect-node"; export class WebSocketPeer extends BasePeer implements Peer { sendMessage(message: Uint8Array): void { @@ -39,7 +39,7 @@ export class WebSocketPeer extends BasePeer implements Peer { public static async connect(uri: URL): Promise { return new Promise((resolve, reject) => { - const socket = new WebSocket(uri); + const socket = isNode ? new WS.WebSocket() : new WebSocket(uri); socket.addEventListener("open", () => { resolve(socket); });