mqtt.fx 国内下载地址
各种mqtt库
https://github.com/mqtt/mqtt.github.io/wiki/libraries?spm=a2c4g.11186623.2.5.5YAnRS
mosquitto 安装
https://mosquitto.org/make & make install
- 问题 编译过程找不到ares.h 打开config.mk文件,把WITH_SRV:=yes 改成WITH_SRV:=no 就可以编译了
- 问题 Invalid user 'mosquitto'
adduser mosquitto
- 问题 libmosquitto.so.1: cannot open shared object file
ldconfig - 问题 uuid/uuid.h
yum install libuuid-devel
mosquitto -c /etc/mosquitto/test.conf -d
mosquitto_passwd -b /etc/mosquitto/test.pwfile dev1 123
mosquitto_passwd -b /etc/mosquitto/test.pwfile admin 123
mosquitto_passwd -b /etc/mosquitto/test.pwfile mqtt_server 123
mosquitto_sub -u admin -P 123 -d -t 'device/#'
mosquitto_pub -u dev1 -P 123 -d -t 'device/dev1/report_info' -m 'xxx'
-r 保留retained
mosquitto
-
retained保留消息
broker保存最后一次topic的保留消息,每当有客户端订阅这个topic,都会收到保留消息
删除保留消息:
方式1:发送空消息体的保留消息;
方式2:发送最新的保留消息覆盖之前的(推荐); -
增加用户
mosquitto_passwd -b /etc/mosquitto/test.pwfile 11010611100000000008 ryrnog
-
删除用户
mosquitto_passwd -D /etc/mosquitto/test.pwfile 11010611100000000008
-
重新load
kill -1 $pid
发送SIGHUP信号,让mosquitto重新load conf -
mosquittoconf
log_dest file /var/log/mosquitto/mosquitto.log
出现Unable to open log file /var/log/mosquitto/mosquitto.log for writing问题
chmod 777 /var/log/mosquitto/
MQTT Control Packet
- Fixed header 固定头 MQTT Control Packet type 控制类型,包括CONNECT,CONNACK,PUBLISH,SUBSCRIBE等等
- Variable header 变量头
- Payload
QOS
- 最多一次(0) 客户端发送一次就不再发了
- 最少一次(1) 客户端会一直重复发送,知道收到服务端的PUBACK
- 只一次(2) 确保消息服务端只会收到一次
php mqtt-client
https://github.com/mgdm/Mosquitto-PHP
- 安装
pecl install Mosquitto-alpha
/usr/local/php/etc/php.ini
extension=mosquitto.so
- loopForever($timeout=1000)
在回调里调用disconnect就会退出
或者exitLoop退出
exitLoop 表示不断开连接退出loopForever
$timeout 表示等待网络,并不是说loop长时间