emqtt:在Erlang中实现的MQTT客户端库和命令行工具,支持MQTT v5.0 / 3.1.1 / 3.1。
下载源码
编译
cd emqtt & make
运行
./rebar3 shell
测试
1、初始化
{ok, ConnPid} = emqtt:start_link([{clientid, "2020"},{keepalive, 0},{proto_ver, v5},{host,"localhost"},{port,1883},{username,"SummerGao"},{password,"123456"}]).
2、建立连接
{ok, _Props} = emqtt:connect(ConnPid).
3、订阅
SubOpts = [{qos, 1}].
{ok, _Props, _ReasonCodes} = emqtt:subscribe(ConnPid, #{}, [{<>, SubOpts}]).
①订阅主题:hello
②用MQTTBox向hello主题推送几条消息:
③flush() 一下查看接收到的消息:
flush().
4、发布
ok = emqtt:publish(ConnPid, <>, #{}, <>, [{qos, 0}]).
发布后,在MQTTBox订阅后可收到推送的消息
5、取消订阅
{ok, _Props, _ReasonCode} = emqtt:unsubscribe(ConnPid, #{}, <>).
6、关闭连接
ok = emqtt:disconnect(ConnPid).
7、退出
ok = emqtt:stop(ConnPid).
测试问题
长时间连接断开:
** exception exit: {shutdown,nxdomain}
备注
{ok, ConnPid} = emqtt:start_link([{clientid, "2020-01-18"}, {owner, self()}, {clean_start, true}, {proto_ver, v5}, {force_ping, true}, {keepalive, 0}, {host, "localhost"}, {port, 1883}, {username, "SummerGao"}, {password, "123456"}]).
工具
参考