From 2d42884dc0110eb7250b30274929e7b5f2e2cac4 Mon Sep 17 00:00:00 2001 From: Hans Date: Thu, 21 Sep 2023 03:13:00 +0800 Subject: [PATCH] refactor: mark session broken when error or close --- src/simple-mq.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/simple-mq.ts b/src/simple-mq.ts index c515efa..9fabcc8 100644 --- a/src/simple-mq.ts +++ b/src/simple-mq.ts @@ -101,15 +101,14 @@ export class SimpleMQBroker extends EventEmitter { protected handleClient(client: Client) { let id: string | undefined; let session: MQClientSession | undefined; - client.on('error', (err) => { - this.emit('error', err); - }); - client.on('close', () => { + const connectionBroken = () => { if (id && session) { delete this.clients[id!]; session!.alive = false; } - }); + }; + client.on('error', connectionBroken); + client.on('close', connectionBroken); client.on('disconnect', () => { client.end(); this.emit('clientDisconnected', id);