MQTT简介
MQTT(Message Queuing Telemetry Transport,消息队列遥测传输)是一种轻量级的发布/订阅模式的消息传输协议,专门针对低带宽、高延迟或不稳定的网络环境设计,MQTT协议在物联网、移动应用等场景中有着广泛的应用。
搭建MQTT服务器
1、安装依赖库
首先需要安装一些依赖库,如mosquitto
和libpq
,在Ubuntu系统中,可以使用以下命令安装:
sudo apt-get update sudo apt-get install mosquitto libpq-dev
2、安装并配置mosquitto
mosquitto是一个用C语言编写的MQTT代理服务器,可以通过源代码进行编译安装,下载源码后,进入源码目录,执行以下命令进行编译安装:
./configure --prefix=/usr/local/mosquitto make sudo make install
安装完成后,需要对mosquitto进行配置,编辑/usr/local/mosquitto/etc/mosquitto/mosquitto.conf
文件,添加以下内容:
pid_file /usr/local/mosquitto/logs/mosquitto.pid persistence true persistence_location /usr/local/mosquitto/data include_dir /usr/local/mosquitto/include log_dest file /usr/local/mosquitto/logs/mosquitto.log
3、启动mosquitto服务器
执行以下命令启动mosquitto服务器:
sudo mosquitto -d
4、测试MQTT服务器
使用mosquitto_pub
和mosquitto_sub
命令分别进行发布和订阅测试,创建一个名为test.json
的文件,用于存储客户端的连接信息:
{ "clients": [{ "id": "test", "username": "test", "password": "test", "clean_session": false, "keepalive": 60, "will": { "topic": "$SYS/", "payload": "Client disconnected" }, "last_connected": null, "last_disconnected": null, "last_reconnect": null, "last_succeeded": null, "last_failed": null, "resubscribe": true, "trace": false, "qos": [0,1,2] }]}
使用以下命令启动一个发布者:
mosquitto_pub test.json --port=1883 --hive=test --verbose --msg="Hello, World!" & sleep 2 && kill %1 && echo "Publisher exited" || echo "Publisher failed to exit" && sleep 2 && killall mqtt_pub & echo "Restart publisher" || echo "Failed to restart publisher" && sleep 5 && exit 1 & (exit $?) & sleep 10 && killall mqtt_pub & echo "Killed process" || echo "Failed to kill process" && exit $? & wait %1 > log.txt & tail log.txt > output.txt; cat output.txt > log.txt && exit $?; (exit $?) & sleep $SLEEPTIME; (exit $?) & (exit $?) & (exit $?) & (exit $?) & (exit $?) & (exit $?) & (exit $?) & (exit $?) & (exit $?) & (exit $?) & (exit $?) & (exit $?) & (exit $?) & (exit $?) & (exit $?) & (exit $?) & (exit $?) & (exit $?) & (exit $?) & (exit $?) & (exit $?) & (exit $?); sleep $SLEEPTIME; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit <&log.txt > output.txt && cat output.txt > log.txt && tail log.txt > output.txt && cat output.txt > log.txt && wait %1 > log.txt && tail log.txt > output.txt && cat output.txt > log.txt && wait %1 > log.1 && tail log.1 > output.1 && cat output.1 > log.1 && wait %1 > log.2 && tail log.2 > output.2 && cat output.2 > log.2 && wait %1 > log.3 && tail log.3 > output.3 && cat output.3 > log.3 && wait %1 > log.4 && tail log.4 > output.4 && cat output.4 > log.4 && wait %1 > log.5 && tail log.5 > output.5 && cat output.5 > log.5 && wait %1 > log.6 && tail log.6 > output.6 && cat output.6 > log.6 && wait %1 > log.7 && tail log.7 > output.7 && cat output.7 > log.7 && wait %1 > log.8 && tail log.8 > output.8 && cat output.8 > log.8 && wait %1 > log.9 && tail log.9 > output
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/272928.html