diff --git a/src/index.ts b/src/index.ts index b39876d..aa858dd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,6 +17,7 @@ export class SwarmClient extends Client { private _ready?: Promise; private _topics: Set = new Set(); + private _sockets: Map = new Map(); get dht() { const self = this; @@ -90,7 +91,14 @@ export class SwarmClient extends Client { "listenConnections", { swarm: this.swarm }, async (socketId: any) => { - this.emit("connection", await createSocket(socketId)); + const socket = + this._sockets.get(socketId) ?? (await createSocket(socketId)); + + socket.on("close", () => { + this._sockets.delete(socketId); + }); + + this.emit("connection", socket); } );