一、 繼上一篇文章<<Mosquitto 介紹&安裝>> 之后、本章介紹 Mosquitto 的 簡單使用。
1> 創建用戶
# groupadd mosquitto
# useradd -g mosquitto mosquitto
2> 程序配置
# mv /etc/mosquitto/mosquitto.conf.example /etc/mosquitto/mosquitto.conf
3> 配置說明
# 服務進程的PID #pid_file /var/run/mosquitto.pid # 服務進程的系統用戶 #user mosquitto # 服務綁定的IP地址 #bind_address # 服務綁定的端口號 #port 1883 # 允許的最大連接數,-1表示沒有限制 #max_connections -1 # 允許匿名用戶 #allow_anonymous true
4> 如果實現websocket 功能 添加如下配置:
mosquitto.conf 的“Default Listener” 一節添加如下幾行: port 1883 listener 9001 protocol websockets
將websocket 端口映射到 9001 上
后續需要使用libwebsockets的庫文件,如果找不到庫,執行下面的命令為庫做一下符號連接(分64位和32位)
ln -s /usr/local/lib/libwebsockets.so.10 /usr/lib64/libwebsockets.so.10
5> 啟動 mosquitto
# mosquitto -c /etc/mosquitto/mosquitto.conf –d
啟動如出現如下錯誤:
.mosquitto: error while loading shared libraries: libwebsockets.so.4.0.0: cannot open shared object file: No such file or directory
是libwebsockets 庫的問題:
ln -s /usr/local/lib64/libwebsockets.so.4.0.0 /usr/lib/libwebsockets.so.4.0.0
還出現上述問題:
vi /etc/ld.so.conf.d/lib64c.conf …and add the following lines:- ==lib64c default configuration /usr/local/lib64 …and then:- ldconfig
成功啟動1883端口監聽
5> 測試:
5.1> 打開兩個shell窗口
5.2> 訂閱一個主題
# mosquitto_sub -t mqtt
5.3> 在另一窗口推送消息
# mosquitto_pub -t mqtt -h localhost -m "new location“
OK 這時會在訂閱窗口看到推送的信息
new location