Skip to content

Commit

Permalink
fix: fix error when unsubscribe with MQTT v5
Browse files Browse the repository at this point in the history
  • Loading branch information
hans00 committed Jun 17, 2023
1 parent 3bd79ca commit 491fded
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/simple-mq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,26 @@ export class SimpleMQBroker extends EventEmitter {
});
client.on('unsubscribe', (packet: IUnsubscribePacket) => {
if (!session) return;
if (Object.keys(session!.subs).length === 0) {
client.unsuback({
messageId: packet.messageId,
reasonCode: 17,
});
return;
}
const granted: number[] = [];
for (const topic of packet.unsubscriptions) {
delete session?.subs[topic];
if (!session!.subs[topic]) {
granted.push(128);
} else {
delete session!.subs[topic];
granted.push(0);
}
}
client.unsuback({
messageId: packet.messageId,
reasonCode: 0,
granted,
});
});
client.on('pingreq', () => {
Expand Down

0 comments on commit 491fded

Please sign in to comment.