diff --git a/src/index.ts b/src/index.ts index c8bd41b..06a63c8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -318,11 +318,21 @@ async function handleListenConnections(aq: ActiveQuery) { aq.respond(); }); - swarm.activeRelay.dht.once("close", () => { + const closeCb = () => { swarm.off("connection", listener); swarm.emit("close"); aq.respond(); - }); + }; + + const hookClose = () => { + swarm.activeRelay.dht._protocol._stream.once("close", closeCb); + }; + + if (swarm.activeRelay) { + hookClose(); + return; + } + swarm.once("ready", hookClose); } async function handleGetSocketInfo(aq: ActiveQuery) {