websocket 连接 mosquitto (MQTT)订阅消息(非常详细)

centos7安装mosquitto并支持websocket

1、下载 mosquitto 和 libwebsockets

https://github.com/eclipse/mosquitto/archive/v1.5.1.tar.gz

https://github.com/warmcat/libwebsockets/archive/v1.5-chrome47-firefox41.tar.gz

2、安装编译工具 gcc和g++

yum install gcc-c++

3、安装mosquitto 编译所需的依赖库

yum install openssl-devel
yum install c-ares-devel
yum install libuuid-devel
yum install e2fsprogs-devel
yum install uuid-devel

4、安装cmake(在安装libwebsockets 的时候需要用到)

yum install cmake

5、解压、编译、安装libwebsockets

 tar zxvf libwebsockets-1.5-chrome47-firefox41.tar.gz 
 cd libwebsockets-1.5-chrome47-firefox41
 mkdir build
 cd build 
 cmake ..
 make
 make install

6、解压、修改mosquitto 的配置config.mk使之支持websocket(默认不支持websocket)

tar zxvf mosquitto-1.5.1.tar.gz
cd mosquitto-1.5.1
//将config.mk 的WITH_WEBSOCKETS:=NO 修改 为 WTIH_WEBSOCKETS:=yes
vim config.mk

7、编译、安装mosquitto

make
make install

(1)编译安装过程中可能会报这个错

make[1]: 进入目录“/home/jason/mosquitto-1.2/man”
xsltproc mosquitto.8.xml
warning: failed to load external entity "/usr/share/xml/docbook/stylesheet/docbook-xsl/manpages/docbook.xsl"
compilation error: file manpage.xsl line 3 element import
xsl:import : unable to load /usr/share/xml/docbook/stylesheet/docbook-xsl/manpages/docbook.xsl
compilation error: file mosquitto.8.xml line 4 element refentry
xsltParseStylesheetProcess : document is not a stylesheet
make[1]: *** [mosquitto.8] 错误 5make[1]:
离开目录“/home/jason/mosquitto-1.2/man”
make: *** [docs] 错误 2

这个因为找不到这个文件/usr/share/xml/docbook/stylesheet/docbook-xsl/manpages/docbook.xsl

,所以需要安装docbook-style-xsl。

yum -y install docbook-style-xsl

(2) 查看docbook-style-xsl的安装位置

find / -name docbook.xsl

发现docbook.xsl是在安装在这里

/usr/share/sgml/docbook/xsl-stylesheets-1.78.1/manpages/docbook.xsl

(3)进入到mosquitto-1.5.1/man的目录,编辑manpage.xsl文件

cd mosquitto-1.5.1/man
vim manpage.xsl 

(4)将红色边框圈中的原始路径修改为

/usr/share/sgml/docbook/xsl-stylesheets-1.78.1/manpages/docbook.xsl

此时再执行make  && make install 即可成功编译、安装。

8、修改mosquitto的配置文件

cd /etc/mosquitto
cp mosquitto.conf.example mosquitto.conf
vim mosquitto.conf

在Default listener 这里添加 

port  1883          

listener 9001

protocol websockets

这里的port  和 listener 都可以自定义,只要没有端口冲突即可。

修改之后保存并退出。

9、添加mosquitto用户,启动mosquitto服务

adduser mosquitto
//mosquitto -c /etc/mosquitto/mosquitto.conf 前台启动方式 
mosquitto -c /etc/mosquitto/mosquitto.conf -d  //后台启动方式(推荐)

启动时如果报如下错误:

是因为没有找到安装的libwebsockets库,执行如下软链接命令:

ln -s /usr/local/lib/libwebsockets.so.5 /usr/lib64/libwebsockets.so.5

然后关闭mosquitto,再重启

 //首先查看mosquitto 的进程id,然后使用kill 命令指定进程id 即可停止mosquitto服务
 //例如我这里的进程id是9307
 ps -ef | grep mos
 kill -9 9307    
 mosquitto -c /etc/mosquitto/mosquitto.conf -d
 ss -tupln | grep 9001 //查看 9001 websocket 端口是否可以监听到

10、关闭防火墙

systemctl stop firewalld.service
systemctl disable firewalld

11、至此便可以编写websocket客户端连接mosquitto服务并成功发布/订阅消息。

参考博客:

https://www.2cto.com/kf/201701/592690.html

https://blog.csdn.net/qq_28877125/article/details/78826550

https://www.cnblogs.com/mao2080/p/7772882.html

https://www.cnblogs.com/smartvessel/archive/2011/01/21/1940868.html

https://blog.csdn.net/houjixin/article/details/46711547

https://www.cnblogs.com/littleatp/p/4835879.html

https://blog.csdn.net/furzoom/article/details/49818333

https://www.cnblogs.com/chen1-kerr/p/7258487.html

https://blog.csdn.net/u012703795/article/details/50388872

https://blog.csdn.net/houjixin/article/details/46711547

https://www.cnblogs.com/djrLog/p/5590245.html

https://blog.csdn.net/xj178926426/article/details/78832296

https://blog.csdn.net/itzaibadong/article/details/49449995

https://blog.csdn.net/lifan1314521/article/details/50402628

https://blog.csdn.net/houjixin/article/details/79789448

https://blog.csdn.net/Post_Yuan/article/details/78603212

猜你喜欢

转载自blog.csdn.net/qq_31905135/article/details/82355440
今日推荐