最近公司使用MQTT对公司现有的物联网设备进行重组,有幸能研究关于MQTT的一个服务端Mosquitto,虽然支持Socket的方式很好部署,但是在WebSoket的部署上还是走了很多弯路,网上查询了很多资料,现在将部署过程中遇到的问题做个总结,方便后续回顾查看:
首先下载Mosquitto安装文件
下载地址:​​​http://mosquitto.org/files/source/​​​
我用的是1.4.8 最新版本进行的部署! 参考部署地址:​​
该博客没有对websocket的支持,下面这篇博客是针对Websocket部署的时候,需要做以下操作:
下载最新的libwebsockets并放在服务器上去编译,下载地址:
​​​https://github.com/warmcat/libwebsockets​​​
下载完成以后 进入libwebsockets目录

mkdir build
cd build
cmake .. -DOPENSSL_ROOT_DIR=/usr/bin/openssl
make
sudo make install

当你安装mosquitto1.4最新版本的时候或多或少会遇到问题。
我是用的centos系统,系统默许很多没有安装
假设你是ubuntu系统请使用 apt-get 代替yum
首先是安装各种工具
yum install gcc gcc-c++ libstdc++-devel
编译进程中问题:
1. ssh.h找不到
yum install openssl-devel
2.ares.h找不到
yum install c-ares-devel
3.如果你添加了websocket
借助git工具 yum install git
wget ​http://git.warmcat.com/cgi-bin/cgit/libwebsockets/snapshot/libwebsockets​⑴.3-chrome37-firefox30.tar.gz
tar -zxvf libwebsockets⑴.3-chrome37-firefox30.tar.gz
cd libwebsockets⑴.3-chrome37-firefox30
mkdir build
cd build
cmake .. -DOPENSSL_ROOT_DIR=/usr/bin/openssl
make
sudo make install
4.uuid.h找不到
yum install libuuid-devel
5.mosquitto: error while loading shared libraries: libwebsockets.so.4.0.0: cannot open shared object file: No such file or directory
库的问题

sudo ln -s /usr/local/lib64/libwebsockets.so.4.0.0 /usr/lib/libwebsockets.so.4.0.0


还不行的话:

sudo vi /etc/ld.so.conf.d/lib64c.conf 
…and add the following lines:-
==lib64c default configuration
/usr/local/lib64
…and then:-
sudo ldconfig

新的Mosquitto版本1.4.2已经支持WebSocket,这就为我们HTML5客户端使用MQTT提供了方法,但是MQTT连接默认不支持WebSocket连接,所以我们要首先设置让Mosquitto支持WebSocket:
(如果遇见有的步骤不能执行或者除错,请使用管理员权限sudo执行)废话不讲,开始:

第一步:安装前准备用到的依赖包:
sudoapt−getupdate sudo apt-get install build-essential python quilt devscripts python-setuptools python3
sudoapt−getinstalllibssl−dev sudo apt-get install cmake
sudoapt−getinstalllibc−ares−dev sudo apt-get install uuid-dev
sudoapt−getinstalldaemon

第二步:下载并编译安装libwebsockets wget ​​​http://git.libwebsockets.org/cgi​​​ … 3-firefox-36.tar.gz
tarzxvflibwebsockets−1.4−chrome43−firefox−36.tar.gz cd libwebsockets-1.4-chrome43-firefox-36
mkdirbuild cd build
cmake.. make install
ldconfig

第三步:下载并编译安装最新版 mosquitto 1.4.2

$ wget ​​http://mosquitto.org/files/source/mosquitto-1.4.2.tar.gz​​​
tarzxvfmosquitto−1.4.2.tar.gz cd mosquitto-1.4.2
更改configure.mk中
WITH_WEBSOCKETS:=no
变成(这一步是做WebSocket支持)

WITH_WEBSOCKETS:=yes
make make install
$ cp mosquitto.conf /etc/mosquitto
第四步:配置Mosquitto能够使用WebSocket

请在/etc/mosquitto/mosquitto.conf 的“Default Listener” 一节添加如下几行:
port 1883
listener 9001
protocol websockets
你添加过后此文件像这样的:

port 1883
listener 9001
protocol websockets
第五步:添加Mosquitto用户:

addusermosquitto reboot
第六步:运行Mosquitto:

$ mosquitto -c /etc/mosquitto/mosquitto.conf
现在你可以试试使用Websocket客户端来连接你的MQTT服务器的9001端口!!!!!!!


常用问题解答

This tutorial explains basic steps of installing mosquitto server on openwrt platform. Mosquitto exists in openwrt barrier breaker repository but I decided to crosscompile it. At host computer in openwrt directory call :

uros-petrevski-2:bin uros$ ./scripts/feeds install mosquitto-nossl
This will install version that don’t support ssl layer because we don’t need it. We need server that is fast. Run make menuconfig and under Network select mosquitto nossl as M package.

uros-petrevski-2:bin uros$ make J=4
Then copy mosquitto package to weio. Mosquitto don’t have any dependencies.

uros-petrevski-2:packages uros$ scp /Volumes/openwrt/carambola/bin/ramips/packages/mosquitto-nossl_1.0.3-1_ramips.ipk root@weio.local:/tmp
Now it’s time to install mosquitto

root@Weio:/tmp# opkg install mosquitto-nossl_1.0.3-1_ramips.ipk -d root
If you run mosquitto now it will break :

root@Weio:/tmp# mosquitto

11145: Error: Invalid user ‘mosquitto’.
The reason is simple, user is not configured. As user is root we have to declare root user un mosquitto configuration. So edit /etc/mosquitto/mosquitto.conf file and add at user section

user root
If you want to run manually mosquitto (you can see what is happening with clients on the console) you have to do :

root@Weio:/# mosquitto -c /etc/mosquitto/mosquitto.conf
Parameter -c specify path to conf file. However if you want to run automaticaly on startup mosquitto server do following

root@Weio:/# /etc/init.d/mosquitto enable
Once system rebooted you can see mosquitto server running by :

root@Weio:/# ps | grep mosquitto
2570 root 932 S /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
2574 root 1488 S grep mosquitto


完成以后测试WebSoket是否成功

下载这个进行测试即可

感谢提供参考资料: ​​
​http://www.tuicool.com/articles/ba6Vja​
​http://www.wfuyu.com/Internet/22690.html