MQTT lua例子
来源:互联网 发布:域名邮箱怎么设置 编辑:程序博客网 时间:2024/04/29 21:14
-- init mqtt client with keepalive timer 120sec
m = mqtt.Client("clientid", 120, "user", "password")
-- setup Last Will and Testament (optional)
-- Broker will publish a message with qos = 0, retain = 0, data = "offline"
-- to topic "/lwt" if client don't send keepalive packet
m:lwt("/lwt", "offline", 0, 0)
m:on("connect", function(con) print ("connected") end)
m:on("offline", function(con) print ("offline") end)
-- on publish message receive event
m:on("message", function(conn, topic, data)
print(topic .. ":" )
if data ~= nil then
print(data)
end
end)
-- for secure: m:connect("192.168.16.9", 1880, 1)
m:connect("192.168.16.9", 1883, 0, function(conn) print("connected") end)
-- subscribe topic with qos = 0
m:subscribe("/topic",0, function(conn) print("subscribe success") end)
-- publish a message with data = hello, QoS = 0, retain = 0
m:publish("/topic","hello",0,0, function(conn) print("sent") end)
m:close();
m = mqtt.Client("clientid", 120, "user", "password")
-- setup Last Will and Testament (optional)
-- Broker will publish a message with qos = 0, retain = 0, data = "offline"
-- to topic "/lwt" if client don't send keepalive packet
m:lwt("/lwt", "offline", 0, 0)
m:on("connect", function(con) print ("connected") end)
m:on("offline", function(con) print ("offline") end)
-- on publish message receive event
m:on("message", function(conn, topic, data)
print(topic .. ":" )
if data ~= nil then
print(data)
end
end)
-- for secure: m:connect("192.168.16.9", 1880, 1)
m:connect("192.168.16.9", 1883, 0, function(conn) print("connected") end)
-- subscribe topic with qos = 0
m:subscribe("/topic",0, function(conn) print("subscribe success") end)
-- publish a message with data = hello, QoS = 0, retain = 0
m:publish("/topic","hello",0,0, function(conn) print("sent") end)
m:close();
-- you can call m:connect again
m = mqtt.Client("clientid", 120, "user", "password");
m:lwt("/lwt", "offline", 0, 0);
m:on("message", function(conn, topic, data) print(topic .. ":" ) if data ~= nil then print(data) end end);
m:connect("192.168.16.9", 1883, 0, function(conn) print("connected") end);
m:connect("impkd.com", 1883, 0, function(conn) print("connected") end);
m:subscribe("/topic",0, function(conn) print("subscribe success") end);
m:publish("/topic","hello",0,0, function(conn) print("sent") end);
0 0
- MQTT lua例子
- Docker+ActiveMQ+MQTT例子
- lua 例子
- MQTT在OPENWRT下的测试例子
- 通过例子学习Lua
- 通过例子学习Lua
- 通过例子学习Lua
- C++调用lua例子
- Lua调用c++例子
- lua入门例子
- lua使用table例子
- 通过例子学习Lua
- nginx-lua开发例子
- lua 语法例子收集
- Lua协程例子
- lua面向对象例子
- 通过例子学习Lua
- mqtt
- OC 总结
- 2057 A + B Again【十六进制】
- 安卓程序段——时间测试函数
- U3D 获取inputfield 的内容
- GitHub控件之BadgeView(数字提醒)
- MQTT lua例子
- Gradle与Makefile构建工具的对比
- exfat文件系统(七)------exfat_core.c详解(二)------file entry相关
- 视频播放插件 swfobject
- hdu 1394 Minimum Inversion Number (树状数组)
- 由xib生成UIView的问题
- $.fn 使用详解
- 用标准IO(带缓冲的IO)实现从stdin复制到stdout
- JSP原理深层解析