Commit dcb6e53a authored by funshine's avatar funshine
Browse files

add auto-reconnect option to mqtt:connect api

parent 09750b56
...@@ -225,8 +225,10 @@ m:on("message", function(conn, topic, data) ...@@ -225,8 +225,10 @@ m:on("message", function(conn, topic, data)
end end
end) end)
-- for secure: m:connect("192.168.11.118", 1880, 1) -- m:connect( host, port, secure, auto_reconnect, function(client) )
m:connect("192.168.11.118", 1880, 0, function(conn) print("connected") end) -- for secure: m:connect("192.168.11.118", 1880, 1, 0)
-- for auto-reconnect: m:connect("192.168.11.118", 1880, 0, 1)
m:connect("192.168.11.118", 1880, 0, 0, function(conn) print("connected") end)
-- subscribe topic with qos = 0 -- subscribe topic with qos = 0
m:subscribe("/topic",0, function(conn) print("subscribe success") end) m:subscribe("/topic",0, function(conn) print("subscribe success") end)
...@@ -235,7 +237,7 @@ m:subscribe("/topic",0, function(conn) print("subscribe success") end) ...@@ -235,7 +237,7 @@ m:subscribe("/topic",0, function(conn) print("subscribe success") end)
-- publish a message with data = hello, QoS = 0, retain = 0 -- publish a message with data = hello, QoS = 0, retain = 0
m:publish("/topic","hello",0,0, function(conn) print("sent") end) m:publish("/topic","hello",0,0, function(conn) print("sent") end)
m:close(); m:close(); -- if auto-reconnect = 1, will reconnect.
-- you can call m:connect again -- you can call m:connect again
``` ```
......
...@@ -7,6 +7,6 @@ ...@@ -7,6 +7,6 @@
#define NODE_VERSION_INTERNAL 0U #define NODE_VERSION_INTERNAL 0U
#define NODE_VERSION "NodeMCU 0.9.5" #define NODE_VERSION "NodeMCU 0.9.5"
#define BUILD_DATE "build 20150330" #define BUILD_DATE "build 20150331"
#endif /* __USER_VERSION_H__ */ #endif /* __USER_VERSION_H__ */
This diff is collapsed.
...@@ -397,6 +397,8 @@ string.gsub("abc%0Ddef", "%%(%x%x)", ex) print("hello") ...@@ -397,6 +397,8 @@ string.gsub("abc%0Ddef", "%%(%x%x)", ex) print("hello")
v="abc%0D%0Adef" v="abc%0D%0Adef"
pcall(function() print(string.gsub(v, "%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end)) end) pcall(function() print(string.gsub(v, "%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end)) end)
mosca -v | bunyan
m=mqtt.Client() m=mqtt.Client()
m:connect("192.168.18.88",1883) m:connect("192.168.18.88",1883)
topic={} topic={}
...@@ -426,3 +428,25 @@ m=mqtt.Client() ...@@ -426,3 +428,25 @@ m=mqtt.Client()
m:on("connect",function(m) print("connection") end ) m:on("connect",function(m) print("connection") end )
m:connect("192.168.18.88",1883) m:connect("192.168.18.88",1883)
m:on("offline",function(m) print("disconnection") end ) m:on("offline",function(m) print("disconnection") end )
m=mqtt.Client()
m:on("connect",function(m) print("connection "..node.heap()) end )
m:on("offline", function(conn)
if conn == nil then print("conn is nil") end
print("Reconnect to broker...")
print(node.heap())
conn:connect("192.168.18.88",1883,0,1)
end)
m:connect("192.168.18.88",1883,0,1)
m=mqtt.Client()
m:on("connect",function(m) print("connection "..node.heap()) end )
m:on("offline", function(conn)
if conn == nil then print("conn is nil") end
print("Reconnect to broker...")
print(node.heap())
conn:connect("192.168.18.88",1883)
end)
m:connect("192.168.18.88",1883)
m:close()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment