diff --git a/packages/app/package.json b/packages/app/package.json index 5742d7071..c7d99aeae 100644 --- a/packages/app/package.json +++ b/packages/app/package.json @@ -38,7 +38,6 @@ "react-tag-input-component": "^2.0.2", "react-textarea-autosize": "^8.4.0", "recharts": "^2.8.0", - "socket.io-client": "^4.7.2", "three": "^0.157.0", "use-long-press": "^3.2.0", "use-sync-external-store": "^1.2.0", diff --git a/packages/app/src/Element/Event/Create/util.ts b/packages/app/src/Element/Event/Create/util.ts index ae2f03b87..cf9bfabce 100644 --- a/packages/app/src/Element/Event/Create/util.ts +++ b/packages/app/src/Element/Event/Create/util.ts @@ -1,6 +1,5 @@ import { NostrEvent, OkResponse, SystemInterface } from "@snort/system"; import { removeUndefined } from "@snort/shared"; -import { getWebRtcPool } from "@/webrtc"; export async function sendEventToRelays( system: SystemInterface, @@ -8,7 +7,6 @@ export async function sendEventToRelays( customRelays?: Array, setResults?: (x: Array) => void, ) { - getWebRtcPool()?.send(ev); if (customRelays) { system.HandleEvent({ ...ev, relays: [] }); return removeUndefined( diff --git a/packages/app/src/index.tsx b/packages/app/src/index.tsx index ad144cd46..98872b44f 100644 --- a/packages/app/src/index.tsx +++ b/packages/app/src/index.tsx @@ -1,7 +1,6 @@ import "./index.css"; import "@szhsin/react-menu/dist/index.css"; import "./fonts/inter.css"; -import "./webrtc"; import { compress, diff --git a/packages/app/src/webrtc/WebRTCConnection.ts b/packages/app/src/webrtc/WebRTCConnection.ts deleted file mode 100644 index 095c993d4..000000000 --- a/packages/app/src/webrtc/WebRTCConnection.ts +++ /dev/null @@ -1,110 +0,0 @@ -import { Socket } from "socket.io-client"; -import EventEmitter from "eventemitter3"; - -export class WebRTCConnection extends EventEmitter { - private peerConnection: RTCPeerConnection; - private dataChannel: RTCDataChannel; - - constructor( - private socket: Socket, - configuration: RTCConfiguration, - public peerId: string, - ) { - super(); - this.peerConnection = new RTCPeerConnection(configuration); - this.dataChannel = this.peerConnection.createDataChannel("data"); - this.registerPeerConnectionEvents(); - this.setupDataChannel(); - } - - private log(...args: T): void { - console.log(this.peerId, ...args); - } - - public async handleOffer(offer: RTCSessionDescriptionInit): Promise { - this.log("Received offer", offer); - await this.peerConnection.setRemoteDescription(new RTCSessionDescription(offer)); - await this.sendLocalDescription("answer"); - } - - public async handleAnswer(answer: RTCSessionDescriptionInit): Promise { - this.log("Received answer", answer); - await this.peerConnection.setRemoteDescription(new RTCSessionDescription(answer)); - } - - public handleCandidate(candidate: RTCIceCandidateInit): void { - this.log("Received ICE candidate", candidate); - this.peerConnection.addIceCandidate(new RTCIceCandidate(candidate)); - } - - private async sendLocalDescription(type: "offer" | "answer"): Promise { - let description; - if (type === "offer") { - description = await this.peerConnection.createOffer(); - } else { - description = await this.peerConnection.createAnswer(); - } - await this.peerConnection.setLocalDescription(description); - this.socket.emit(type, { [type]: description, recipient: this.peerId }); - this.log(`Sent ${type}`, description); - } - - private setupDataChannel(): void { - this.dataChannel.onopen = () => this.log("Data channel opened"); - this.dataChannel.onclose = () => this.log("Data channel closed"); - this.dataChannel.onmessage = event => this.handleDataChannelMessage(event); - } - - private handleDataChannelMessage(event: MessageEvent): void { - this.log(`-> "${event.data}"`); - if (event.data === "ping") { - this.send("pong"); - } else { - try { - const data = JSON.parse(event.data); - this.emit("event", { ...data, relays: [`webrtc:${this.peerId}`] }); - } catch (e) { - // Ignore - } - } - } - - public send(data: string): void { - if (this.dataChannel.readyState === "open") { - this.log(`<- "${data}"`); - this.dataChannel.send(data); - } - } - - public async handleHello(): Promise { - if (this.peerConnection.connectionState === "new") { - await this.sendLocalDescription("offer"); - } - } - - private registerPeerConnectionEvents(): void { - this.peerConnection.onicecandidate = event => { - if (event.candidate) { - this.log("Local ICE candidate:", event.candidate); - this.socket.emit("candidate", { candidate: event.candidate.toJSON(), recipient: this.peerId }); - } - }; - - this.peerConnection.oniceconnectionstatechange = () => { - this.log("ICE Connection State Change:", this.peerConnection.iceConnectionState); - }; - - this.peerConnection.onconnectionstatechange = () => { - this.log("WebRTC Connection State Change:", this.peerConnection.connectionState); - }; - - this.peerConnection.ondatachannel = event => { - this.dataChannel = event.channel; - this.setupDataChannel(); - }; - } - - public close(): void { - this.peerConnection.close(); - } -} diff --git a/packages/app/src/webrtc/WebRTCPool.ts b/packages/app/src/webrtc/WebRTCPool.ts deleted file mode 100644 index f5ed5f65a..000000000 --- a/packages/app/src/webrtc/WebRTCPool.ts +++ /dev/null @@ -1,90 +0,0 @@ -import { io, Socket } from "socket.io-client"; -import { WebRTCConnection } from "@/webrtc/WebRTCConnection"; -import EventEmitter from "eventemitter3"; -import { TaggedNostrEvent } from "@snort/system"; - -const MAX_CONNECTIONS = 5; - -class WebRTCPool extends EventEmitter { - private signalingServer: Socket; - private peers: Map = new Map(); - private configuration: RTCConfiguration; - private peerId: string; - - constructor(serverUrl: string, configuration: RTCConfiguration = {}, peerId: string) { - super(); - this.signalingServer = io(serverUrl); - this.configuration = configuration; - this.peerId = peerId; - this.registerSocketEvents(); - } - - private sayHello(): void { - this.signalingServer.emit("hello", this.peerId); - } - - public send(data: TaggedNostrEvent | string, recipients?: string[]): void { - this.peers.forEach(conn => { - if (!recipients || recipients.includes(conn.peerId)) { - try { - conn.send(typeof data === "string" ? data : JSON.stringify(data)); - } catch (e) { - console.error(e); - } - } - }); - } - - public createConnection(peerId: string): WebRTCConnection { - if (this.peers.size >= MAX_CONNECTIONS) { - throw new Error("Maximum connections reached"); - } - const connection = new WebRTCConnection(this.signalingServer, this.configuration, peerId); - connection.on("event", (event: TaggedNostrEvent | string) => this.emit("event", event)); - this.peers.set(peerId, connection); - return connection; - } - - private handleConnectionEvent(sender: string, action: (connection: WebRTCConnection) => Promise): void { - if (sender === this.peerId || this.peers.size >= MAX_CONNECTIONS) return; - const connection = this.peers.get(sender) ?? this.createConnection(sender); - action(connection); - } - - private registerSocketEvents(): void { - this.signalingServer.on("connect", () => { - console.log("Connected to signaling server"); - this.sayHello(); - }); - - this.signalingServer.on("offer", ({ offer, sender }: { offer: RTCSessionDescriptionInit; sender: string }) => { - this.handleConnectionEvent(sender, async conn => await conn.handleOffer(offer)); - }); - - this.signalingServer.on("answer", ({ answer, sender }: { answer: RTCSessionDescriptionInit; sender: string }) => { - this.handleConnectionEvent(sender, async conn => await conn.handleAnswer(answer)); - }); - - this.signalingServer.on( - "candidate", - ({ candidate, sender }: { candidate: RTCIceCandidateInit; sender: string }) => { - this.handleConnectionEvent(sender, conn => conn.handleCandidate(candidate)); - }, - ); - - this.signalingServer.on("hello", (sender: string) => { - console.log("Received hello from", sender); - this.handleConnectionEvent(sender, conn => conn.handleHello()); - }); - } - - public close(): void { - console.log("closing pool"); - this.signalingServer.close(); - for (const conn of this.peers.values()) { - conn.close(); - } - } -} - -export default WebRTCPool; diff --git a/packages/app/src/webrtc/index.ts b/packages/app/src/webrtc/index.ts deleted file mode 100644 index 89f5f6068..000000000 --- a/packages/app/src/webrtc/index.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { LoginStore } from "@/Login"; -import WebRTCPool from "@/webrtc/WebRTCPool"; -import { System } from "@/index"; -import { TaggedNostrEvent } from "@snort/system"; - -let publicKey: string | undefined; -let pool: WebRTCPool | undefined; -let interval: NodeJS.Timeout | undefined; - -LoginStore.hook(() => { - const login = LoginStore.takeSnapshot(); - if (!login.publicKey || login.readonly || login.publicKey === publicKey) return; - publicKey = login.publicKey; - if (location.hostname === "localhost") { - pool?.close(); - interval && clearInterval(interval); - pool = new WebRTCPool( - "http://localhost:3000", - { - iceServers: [{ urls: "stun:localhost:3478" }], - }, - login.publicKey, - ); - pool.on("event", (event: TaggedNostrEvent) => { - console.log("event from webrtc", event); - System.HandleEvent(event); - }); - interval = setInterval(() => pool?.send("ping"), 10000); - } -}); - -export function getWebRtcPool(): WebRTCPool | undefined { - return pool; -} diff --git a/yarn.lock b/yarn.lock index 973e39c07..3486a21b1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2984,7 +2984,6 @@ __metadata: react-textarea-autosize: ^8.4.0 recharts: ^2.8.0 rollup-plugin-visualizer: ^5.9.2 - socket.io-client: ^4.7.2 tailwindcss: ^3.3.3 three: ^0.157.0 tinybench: ^2.5.1 @@ -5528,19 +5527,6 @@ __metadata: languageName: node linkType: hard -"engine.io-client@npm:~6.5.2": - version: 6.5.3 - resolution: "engine.io-client@npm:6.5.3" - dependencies: - "@socket.io/component-emitter": ~3.1.0 - debug: ~4.3.1 - engine.io-parser: ~5.2.1 - ws: ~8.11.0 - xmlhttprequest-ssl: ~2.0.0 - checksum: a72596fae99afbdb899926fccdb843f8fa790c69085b881dde121285a6935da2c2c665ebe88e0e6aa4285637782df84ac882084ff4892ad2430b059fc0045db0 - languageName: node - linkType: hard - "engine.io-parser@npm:~5.2.1": version: 5.2.1 resolution: "engine.io-parser@npm:5.2.1" @@ -10138,18 +10124,6 @@ __metadata: languageName: node linkType: hard -"socket.io-client@npm:^4.7.2": - version: 4.7.2 - resolution: "socket.io-client@npm:4.7.2" - dependencies: - "@socket.io/component-emitter": ~3.1.0 - debug: ~4.3.2 - engine.io-client: ~6.5.2 - socket.io-parser: ~4.2.4 - checksum: 8f0ab6b623e014d889bae0cd847ef7826658e8f131bd9367ee5ae4404bb52a6d7b1755b8fbe8e68799b60e92149370a732b381f913b155e40094facb135cd088 - languageName: node - linkType: hard - "socket.io-parser@npm:~4.2.4": version: 4.2.4 resolution: "socket.io-parser@npm:4.2.4" @@ -11820,13 +11794,6 @@ __metadata: languageName: node linkType: hard -"xmlhttprequest-ssl@npm:~2.0.0": - version: 2.0.0 - resolution: "xmlhttprequest-ssl@npm:2.0.0" - checksum: 1e98df67f004fec15754392a131343ea92e6ab5ac4d77e842378c5c4e4fd5b6a9134b169d96842cc19422d77b1606b8df84a5685562b3b698cb68441636f827e - languageName: node - linkType: hard - "y18n@npm:^5.0.5": version: 5.0.8 resolution: "y18n@npm:5.0.8"