From 0fae036609cb7537304b01e8c7d13784413264bb Mon Sep 17 00:00:00 2001 From: Luca Date: Thu, 5 Sep 2024 21:45:14 +0200 Subject: [PATCH] feat: server disconnect handling --- mqtt.lua | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/mqtt.lua b/mqtt.lua index 6a895ce..f16bd7c 100644 --- a/mqtt.lua +++ b/mqtt.lua @@ -131,12 +131,27 @@ function MqttClient:handle () self.is_connecting = false self.is_connected = true - elseif ptype == 0x40 then -- PUBACK + elseif ptype & 0xF0 == 0x40 then -- PUBACK -- TODO - elseif ptype == 0xD0 then -- PINGRESP - -- TODO - elseif ptype == 0xE0 then -- DISCONNECT + elseif ptype & 0xF0 == 0xD0 then -- PINGRESP -- TODO + elseif ptype & 0xF0 == 0xE0 then -- DISCONNECT + if ptype ~= 0xE0 then + self:disconnect(0x81) + return "malformed packet" + end + + self.is_connecting = false + self.is_connected = false + + if length > 0 then + reason, _ = string.unpack("B", data) + if reason ~= 0 then + return "disconnect with error" + end + end + + return "disconnect" end return nil