From 2ad6b1fd5d810a5c3a847fae236c76c81e3bd5ab Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Fri, 17 Feb 2023 22:37:05 -0500 Subject: [PATCH] *Wrap connection event in an init call event to survive the relay being recreated --- src/index.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 06a63c8..49f72a4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -80,12 +80,14 @@ async function createSwarm(): Promise { const id = getSwarmId(); swarmInstances.set(id, swarmInstance); - swarmInstance.on("connection", (peer) => { - const socketId = getSocketId(); - connections.set(socketId, { swarm: id, conn: peer }); + swarmInstance.onSelf("init", () => { + swarmInstance.on("connection", (peer: any) => { + const socketId = getSocketId(); + connections.set(socketId, { swarm: id, conn: peer }); - peer.on("close", () => { - connections.delete(socketId); + peer.on("close", () => { + connections.delete(socketId); + }); }); }); @@ -308,6 +310,7 @@ async function handleListenConnections(aq: ActiveQuery) { const swarm = await getSwarm(aq); const listener = (peer: any) => { + debugger; aq.sendUpdate(getSwarmToSocketConnectionId(peer)); };