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出錯資訊 |
| | |
| | |
| 對于需要使用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/

作者:
zale出處:
http://www.cnblogs.com/littleatp/, 如果喜歡我的文章,請
關注我的公衆号本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出
原文連結如有問題, 可留言咨詢.