Initial receive support in mymqtt
This commit is contained in:
parent
f639cbf4db
commit
b824f18df9
@ -242,8 +242,33 @@ function clientMt:subscribe(args)
|
|||||||
return self:sendPacket()
|
return self:sendPacket()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function clientMt:receiveBytes(count)
|
||||||
|
local result, err, partial = nil, nil, ""
|
||||||
|
while true do
|
||||||
|
result, err, partial = client:receive(1 - #partial, partial)
|
||||||
|
if err == "timeout" then
|
||||||
|
coroutine.yield()
|
||||||
|
else if result then
|
||||||
|
return result
|
||||||
|
else
|
||||||
|
return nil, err
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function clientMt:receiveByte()
|
||||||
|
return string.byte(self:receiveBytes(1)))
|
||||||
|
end
|
||||||
|
|
||||||
function clientMt:receivePacket()
|
function clientMt:receivePacket()
|
||||||
local firstByte = self:receiveByte()
|
local firstByte, err = self:receiveByte()
|
||||||
|
if err then return nil, self:handleError(err) end
|
||||||
|
end
|
||||||
|
|
||||||
|
function clientMt:threadReceive()
|
||||||
|
while true do
|
||||||
|
self:receiveAndHandlePacket()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function clientMt:on(eventHandlers)
|
function clientMt:on(eventHandlers)
|
||||||
@ -257,9 +282,10 @@ function mqtt.client(args)
|
|||||||
reconnect = 5,
|
reconnect = 5,
|
||||||
connection = nil,
|
connection = nil,
|
||||||
packetIdentifier = 1,
|
packetIdentifier = 1,
|
||||||
buffer = "",
|
buffer = ""
|
||||||
}
|
}
|
||||||
setmetatable(client, {__index = clientMt})
|
setmetatable(client, {__index = clientMt})
|
||||||
|
client.create(function() client:threadReceive() end)
|
||||||
return client
|
return client
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user