搭建步骤如下:
1、安装依赖包
yum -y install gcc gcc-c++ openssl-devel c-ares-devel libuuid-devel wget cmake
2、安装libwebsockets(mosquitto支持websocket)
命令如下:
cd /mnt/
git clone https://git.oschina.net/woniu201/libwebsockets.git
cd libwebsockets/
tar zxfv libwebsockets-v1.5-stable.tar.gz
cd libwebsockets-v1.5-stable
mkdir build
cd build/
cmake ..
make && make install
3、安装mosquitto
wget http://mosquitto.org/files/source/mosquitto-1.4.8.tar.gz
tar zxfv mosquitto-1.4.8.tar.gz
cd mosquitto-1.4.8
vi config.mk
WITH_WEBSOCKETS:=no 改为yes
make && make install
ln -s /usr/local/lib/libwebsockets.so.6 /usr/lib64/libwebsockets.so.6
groupadd mosquitto
useradd -g mosquitto mosquitto
cd /etc/mosquitto/
创建mosquitto.conf、pwfile文件
# cp mosquitto.conf.example mosquitto.conf
# cp pwfile.example pwfile
修改 文件mosquitto.conf
并在最后添加:
port 1883
listener 9109
protocol websockets
4、启动
mosquitto -d -c /etc/mosquitto/mosquitto.conf
报错:mosquitto: error while loading shared libraries: libwebsockets.so.5: cannot open shared object file: No such file or directory
处理方法:
[root@Docker mosquitto]# cat /etc/ld.so.conf
include ld.so.conf.d/*.conf
[root@Docker mosquitto]# echo “/usr/local/lib”>>/etc/ld.so.conf
[root@Docker mosquitto]# ldconfig
5、测试
开两个窗口
mosquitto_sub -t mqtt (订阅)
mosquitto_pub -h localhost -t mqtt -m “hello world”(发送消息)
# -c是指定一个配置文件 # -d作为守护进程 # -p是指定端口,默认1883
# -v详细日志输出
6、设置密码
a、修改/etc/mosquitto/mosquitto.conf
allow_anonymous false
b、新建用户
mosquitto_passwd -c /etc/mosquitto/pwfile test
c、测试命令
mosquitto_sub -t mqtt -u test -P t123456
mosquitto_pub -h localhost -t mqtt -m “hello world” -u test -P t123456
搭建支持websocket,主要是小程序的开发中需要用到此技术点。此篇文章主要是现实了第一步,下一篇文章会介绍小程序中使用websocket协议连接mqtt服务器。
参考资料:
http://blog.csdn.net/u011033736/article/details/53218394
http://blog.csdn.net/u012377333/article/details/69397124