Centos7 Mosquitto install totorua
yum install mosquitto
cd /etc/mosquitto
cp pwfile.example pwfile
mosquitto_passwd -c /etc/mosquitto/pwfile seven
cp aclfile.example aclfile
vi aclfile
#覆蓋
user seven
topic read test/#
topic write test/# //test 為 topic主題
vi mosquitto.conf
# 服務行程的PID
pid_file /var/run/mosquitto.pid
# 服務行程的系統使用者
user root
# 服務繫結的埠號
port 1883
# 允許的最大連接數,-1表示沒有限制
max_connections -1
# 允許匿名使用者
allow_anonymous false
# 使用者/密碼檔案,預設格式:username:password
password_file /etc/mosquitto/pwfile
# 配置使用者訪問控制
acl_file /etc/mosquitto/aclfile
# 訊息持久儲存
persistence true
persistence_location /var/lib/mosquitto/
# 日誌檔案
log_dest file /var/log/mosquitto/mosquitto.log
啟動mosquitto服務
mosquitto -c /etc/mosquitto/mosquitto.conf -d
註:如果啟動失敗,用mosquitto -c /etc/mosquitto/mosquitto.conf -v除錯啟動,再者檢視日誌,伺服器要開放1883連接埠和防火牆
測試
開啟兩個命令視窗
訂閱
mosquitto_sub -t test/unreal -u unreal -P 123456
釋出
mosquitto_pub -t "test/unreal" -m "How are you?" -u unreal -P 123456
註:測試釋出的時候主題要用引號括起來,否則訂閱者收不到
網上一個比較好的除錯工具
開放websocket連接,
# 允許websocket連接
listener 9000
protocol websockets