Mosquitto
mosquitto是一款實現了 MQTT v3.1 協議的開源的消息代理服務軟件.
其提供了非常輕量級的消息數據傳輸協議,采用發布/訂閱模式進行工作,可用於物聯設備、中間件、APP客戶端之間的消息通訊。
mosquitto官網
http://mosquitto.org/
關於mqtt協議可參考
http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html
基礎准備
Linux內核版本:Centos 6.5_final_64bit
安裝基礎軟件
yum install gcc-c++ yum install cmake yum install openssl-devel //mosquitto默認支持openssl
下載程序
官網下載
wget http://mosquitto.org/files/source/mosquitto-1.4.4.tar.gz tar -xzvf mosquitto-1.4.4.tar.gz cd mosquitto-1.4.4
編譯安裝
編譯選項
當前的程序目錄可直接編譯,在編譯之前需根據需要做一定的配置,否則會出現 xxx.h找不到的情況。
vim config.mk
config.mk包括了多個選項, 可按需關閉或開啟,但一旦開啟則需要先安裝對應的模塊
模塊說明
選項
|
說明
|
make出錯信息
|
WITH_SRV
|
啟用c-areas庫的支持,一個支持異步DNS查找的庫 見http://c-ares.haxx.se |
missing ares.h
|
WITH_UUID
|
啟用lib-uuid支持,支持為每個連接的客戶端生成唯一的uuid
|
missing uuid.h
|
WITH_WEBSOCKETS
|
啟用websocket支持,需安裝libwebsockets 對於需要使用websocket協議的應用開啟 |
missing libwebsockets.h
|
安裝c-areas
wget http://c-ares.haxx.se/download/c-ares-1.10.0.tar.gz tar xvf c-ares-1.10.0.tar.gz cd c-ares-1.10.0 ./configure make sudo make install
安裝lib-uuid
yum install libuuid-devel
安裝libwebsockets
wget https://github.com/warmcat/libwebsockets/archive/v1.3-chrome37-firefox30.tar.gz tar zxvf v1.3-chrome37-firefox30.tar.gz cd libwebsockets-1.3-chrome37-firefox30 mkdir build; cd build; cmake .. -DLIB_SUFFIX=64 make install
//若遇到以上模塊無法安裝的情況,可將對應模塊選項關閉即可,但相應功能也將無法提供;
開始安裝mosquitto
make make install
至此程序已經安裝完畢!
程序文件將默認安裝到以下位置
路徑 | 程序文件 |
/usr/local/sbin | mosquiotto server |
/etc/mosquitto | configuration |
/usr/local/bin | utility command |
修正鏈接庫路徑
由於操作系統版本及架構原因,很容易出現安裝之后的鏈接庫無法被找到,如啟動mosquitto客戶端可能出現找不到
libmosquitto.so.1文件,因此需要添加鏈接庫路徑
//添加路徑 vim /etc/ld.so.conf.d/liblocal.conf /usr/local/lib64 /usr/local/lib
//刷新 ldconfig
啟動與測試
創建用戶
mosquitto默認以mosquitto用戶啟動,可以通過配置文件修改
groupadd mosquitto
useradd -g mosuqitto mosquiotto
程序配置
mv /etc/mosquitto/mosquitto.conf.example /etc/mosquitto/mosquitto.conf
配置項說明
# 服務進程的PID #pid_file /var/run/mosquitto.pid # 服務進程的系統用戶 #user mosquitto # 服務綁定的IP地址 #bind_address # 服務綁定的端口號 #port 1883 # 允許的最大連接數,-1表示沒有限制 #max_connections -1 # 允許匿名用戶 #allow_anonymous true
//關於詳細配置可參考
http://mosquitto.org/man/mosquitto-conf-5.html
啟動
mosquitto -c /etc/mosquitto/mosquitto.conf -d
成功將啟動1883端口監聽
客戶端測試
新建兩個shell端口A/B
A 訂閱主題:
mosquitto_sub -t location
B 推送消息:
mosquitto_pub -t location -h localhost -m "new location"
可以在A窗口看到由B推送的消息,此外服務端窗口也可以看到客戶端連接和端口的日志
1443083396: New client connected from 127.0.0.1 as mosqpub/31924-iZ94eb8yq (c1, k60). 1443083396: Client mosqpub/31924-iZ94eb8yq disconnected.、
FAQ
啟動mosquitto報錯
error while loading shared libraries: libwebsockets.so.4.0.0: cannot open shared object file: No such file or directory
或者
error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory
解決方法
找不到鏈接庫,通過locate或find命令找到libwebsockets.so.4.0.0,將其目錄添加至ldconfg配置中:
vim /etc/ld.so.conf.d/liblocal.conf /usr/local/lib64 /usr/local/lib ldconfig //執行ln -s 添加軟連接的方式也可行
編譯找不到openssl/ssl.h
解決方法
yum install openssl-devel
編譯報錯
mosquitto.c:871: error: ‘struct mosquitto’ has no member named ‘achan’
找不到areas.h
解決方法
安裝 c-areas模塊(見上文)或將config.mk中WITH_SRV選項關閉
make test 提示不支持協議
Address family not supported by protocol
一般是指所訪問的地址類型不被支持,比如IPV6,忽略該錯誤即可
參考文檔
mosquitto1.4 搭建日記
https://goochgooch.wordpress.com/2014/08/01/building-mosquitto-1-4/
Ubuntu下搭建教程(日文)
http://qiita.com/aquaviter/items/cb3051cf42a3a3c4a4d9
使mosquitto支持websockets
https://www.justinribeiro.com/chronicle/2014/10/22/mosquitto-libwebsockets-google-compute-engine-setup/
使用JS實現mqtt-websocket
http://jpmens.net/2014/07/03/the-mosquitto-mqtt-broker-gets-websockets-support/