Library you found is cool and allows you to specify custom transport for mqtt client.
Just pass the connector with wrapped cosocket while creating mqtt client.
local client = mqtt.client{ uri = broker_url, clean = true, connector = require "mqtt.ngxsocket" }
Implementation that works for me:
-- ngxsocket.lua
-- module table
local luasocket = {}
-- Open network connection to .host and .port in conn table
-- Store opened socket to conn table
-- Returns true on success, or false and error text on failure
function luasocket.connect(conn)
local socket = ngx.socket.tcp()
local sock, err = socket:connect(conn.host, conn.port)
if not sock then
return false, "socket.connect failed: "..err
end
conn.sock = socket
return true
end
-- Shutdown network connection
function luasocket.shutdown(conn)
local ok, err = conn.sock:close()
end
-- Send data to network connection
function luasocket.send(conn, data, i, j)
return conn.sock:send(data)
end
-- Receive given amount of data from network connection
function luasocket.receive(conn, size)
conn.sock:settimeout(1000)
local ok, err = conn.sock:receive(size)
return ok, err
end
-- export module table
return luasocket
пятница, 22 марта 2019 г., 16:44:57 UTC+3 пользователь bogdan написал:
Hello, everybody
I am looking for an implementation of a Lua MQTT client (to work in
OpenResty). All I could find is this: https://github.com/xHasKx/luamqtt.
It uses "luasocket" and I was wondering if there is anything similar
written for OpenResty (with OpenResty's cosocket implementation).
What would be your recommendation? Should I use the module from xHasKx?
How should I install the luasocket module?
Thank you