Working on better mqtt support
This commit is contained in:
parent
cec28500b3
commit
c7344711c2
@ -38,7 +38,6 @@ local function onCommand(command)
|
|||||||
assert(client:subscribe {
|
assert(client:subscribe {
|
||||||
topic = topic
|
topic = topic
|
||||||
})
|
})
|
||||||
})
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -257,7 +257,7 @@ function clientMt:receiveBytes(count)
|
|||||||
result, err, partial = client:receive(1 - #partial, partial)
|
result, err, partial = client:receive(1 - #partial, partial)
|
||||||
if err == "timeout" then
|
if err == "timeout" then
|
||||||
coroutine.yield()
|
coroutine.yield()
|
||||||
else if result then
|
elseif result then
|
||||||
return result
|
return result
|
||||||
else
|
else
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -266,7 +266,7 @@ function clientMt:receiveBytes(count)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function clientMt:receiveByte()
|
function clientMt:receiveByte()
|
||||||
return string.byte(self:receiveBytes(1)))
|
return string.byte(self:receiveBytes(1))
|
||||||
end
|
end
|
||||||
|
|
||||||
function clientMt:receiveVarInt()
|
function clientMt:receiveVarInt()
|
||||||
@ -293,6 +293,7 @@ function clientMt:receivePacket()
|
|||||||
if err then return self:handleError(err) end
|
if err then return self:handleError(err) end
|
||||||
|
|
||||||
local packetType = (firstByte >> 4) & 0xF
|
local packetType = (firstByte >> 4) & 0xF
|
||||||
|
print("Got packet of type " .. packetType)
|
||||||
if packetType == 2 then
|
if packetType == 2 then
|
||||||
-- CONNACK
|
-- CONNACK
|
||||||
assert(remainingLength == 2, "Invalid CONNACK length")
|
assert(remainingLength == 2, "Invalid CONNACK length")
|
||||||
@ -313,16 +314,28 @@ function clientMt:receivePacket()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function clientMt:threadReceive()
|
function clientMt:threadReceive()
|
||||||
while true do
|
-- local status, err = pcall(function()
|
||||||
if self.connection then
|
while true do
|
||||||
local err = self:receiveAndHandlePacket()
|
if self.connection then
|
||||||
if err then
|
local err = self:receiveAndHandlePacket()
|
||||||
self:handleError(err)
|
if err then
|
||||||
end
|
self:handleError(err)
|
||||||
else
|
end
|
||||||
coroutine.yield()
|
else
|
||||||
end
|
coroutine.yield()
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
-- end)
|
||||||
|
-- if err then
|
||||||
|
-- print(err)
|
||||||
|
-- error(err)
|
||||||
|
-- end
|
||||||
|
end
|
||||||
|
|
||||||
|
function clientMt:runForever()
|
||||||
|
while true do
|
||||||
|
coroutine.resume(self.thread)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function clientMt:on(eventHandlers)
|
function clientMt:on(eventHandlers)
|
||||||
@ -339,7 +352,7 @@ function mqtt.client(args)
|
|||||||
buffer = ""
|
buffer = ""
|
||||||
}
|
}
|
||||||
setmetatable(client, {__index = clientMt})
|
setmetatable(client, {__index = clientMt})
|
||||||
client.create(function() client:threadReceive() end)
|
client.thread = coroutine.create(function() client:threadReceive() end)
|
||||||
return client
|
return client
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
package.path = package.path .. ';./controller-host/?/init.lua;./controller-host/?.lua'
|
package.path = package.path .. ';./controller-host/?/init.lua;./controller-host/?.lua'
|
||||||
local mqtt = require("mqtt")
|
local mqtt = require("mymqtt")
|
||||||
local client
|
local client
|
||||||
|
|
||||||
local host = ...
|
local host = ...
|
||||||
@ -39,4 +39,6 @@ client:on {
|
|||||||
message = onMessage,
|
message = onMessage,
|
||||||
}
|
}
|
||||||
|
|
||||||
mqtt.run_ioloop(client)
|
client:runForever()
|
||||||
|
|
||||||
|
--mqtt.run_ioloop(client)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user