feat: implement QoS 0 PUBLISH

This commit is contained in:
Luca 2024-09-05 22:02:45 +02:00
parent 5d0c1bdec4
commit e014cb2656
1 changed files with 29 additions and 0 deletions

View File

@ -200,6 +200,35 @@ function MqttClient:connect (username, password)
return nil return nil
end 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) function MqttClient:disconnect (reason)
if not (self.is_connecting or self.is_connected) then if not (self.is_connecting or self.is_connected) then
return "no connection" return "no connection"