From e014cb2656387a3e9e5055682ccba6d2d068f3c6 Mon Sep 17 00:00:00 2001 From: Luca Date: Thu, 5 Sep 2024 22:02:45 +0200 Subject: [PATCH] feat: implement QoS 0 PUBLISH --- mqtt.lua | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/mqtt.lua b/mqtt.lua index b5f034f..c8802eb 100644 --- a/mqtt.lua +++ b/mqtt.lua @@ -200,6 +200,35 @@ function MqttClient:connect (username, password) return nil end +function MqttClient:publish (topic, payload) + if not (self.is_connecting or self.is_connected) then + return "no connection" + end + + if not topic or #topic == 0 then + return "topic is required" + end + if not payload then + payload = "" + end + + local flags = 0 + local length = 3 + #topic + #payload + local data = string.char(0x30 | flags) .. encodeVarint(length) .. string.pack("> s2 B", topic, 0) .. payload + + local _, err = self.conn:write(data) + if err ~= nil then + return err + end + + local _, err = self.conn:flush() + if err ~= nil then + return err + end + + return nil +end + function MqttClient:disconnect (reason) if not (self.is_connecting or self.is_connected) then return "no connection"