fix: add support for browser WS by checking if we are in node

This commit is contained in:
Derrick Hammer 2023-09-01 06:19:56 -04:00
parent 76879734ea
commit 0a153379c8
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 3 additions and 3 deletions

View File

@ -1,8 +1,8 @@
import { Logger, Peer } from "../types.js"; import { Logger, Peer } from "../types.js";
import NodeId from "../nodeId.js";
import { URL } from "url"; import { URL } from "url";
import { WebSocket } from "ws"; import * as WS from "ws";
import { BasePeer } from "#transports/base.js"; import { BasePeer } from "#transports/base.js";
import isNode from "detect-node";
export class WebSocketPeer extends BasePeer implements Peer { export class WebSocketPeer extends BasePeer implements Peer {
sendMessage(message: Uint8Array): void { sendMessage(message: Uint8Array): void {
@ -39,7 +39,7 @@ export class WebSocketPeer extends BasePeer implements Peer {
public static async connect(uri: URL): Promise<WebSocket> { public static async connect(uri: URL): Promise<WebSocket> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const socket = new WebSocket(uri); const socket = isNode ? new WS.WebSocket() : new WebSocket(uri);
socket.addEventListener("open", () => { socket.addEventListener("open", () => {
resolve(socket); resolve(socket);
}); });