Improve MQTT client example, fixes #1792

This commit is contained in:
Marcel Stör 2017-02-12 17:04:37 +01:00
parent 4dfa5cd7d6
commit b26ed97246
1 changed files with 15 additions and 11 deletions

View File

@ -48,18 +48,22 @@ m:on("message", function(client, topic, data)
end) end)
-- for TLS: m:connect("192.168.11.118", secure-port, 1) -- for TLS: m:connect("192.168.11.118", secure-port, 1)
m:connect("192.168.11.118", 1883, 0, function(client) print("connected") end, m:connect("192.168.11.118", 1883, 0, function(client)
function(client, reason) print("failed reason: "..reason) end) print("connected")
-- Calling subscribe/publish only makes sense once the connection
-- was successfully established. You can do that either here in the
-- 'connect' callback or you need to otherwise make sure the
-- connection was established (e.g. tracking connection status or in
-- m:on("connect", function)).
-- Calling subscribe/publish only makes sense once the connection -- subscribe topic with qos = 0
-- was successfully established. In a real-world application you want client:subscribe("/topic", 0, function(client) print("subscribe success") end)
-- move those into the 'connect' callback or make otherwise sure the -- publish a message with data = hello, QoS = 0, retain = 0
-- connection was established. client:publish("/topic", "hello", 0, 0, function(client) print("sent") end)
end,
-- subscribe topic with qos = 0 function(client, reason)
m:subscribe("/topic",0, function(client) print("subscribe success") end) print("failed reason: " .. reason)
-- publish a message with data = hello, QoS = 0, retain = 0 end)
m:publish("/topic","hello",0,0, function(client) print("sent") end)
m:close(); m:close();
-- you can call m:connect again -- you can call m:connect again