https://github.com/eclipse/mosquitto
https://github.com/warmcat/libwebsockets
进入软件安装目录:
cd /opt/software
下载源码包:
wget https://github.com/eclipse/mosquitto/archive/v1.5.1.tar.gz
wget https://github.com/warmcat/libwebsockets/archive/v1.5-chrome47-firefox41.tar.gz
解压安装libwebsockets
tar -zxf v1.5-chrome47-firefox41.tar.gz
cd v1.5-chrome47-firefox41
mkdir build
cd build
cmake ..
make
make install
解压、修改mosquitto的配置config.mk使之支持websocket(默认不支持websocket)
tar -zxf v1.5.1.tar.gz
cd v1.5.1
# 将config.mk 的WITH_WEBSOCKETS:=NO 修改 为 WTIH_WEBSOCKETS:=yes
vim config.mk
先安装其它依赖库:
yum install -y c-ares-devel e2fsprogs-devel uuid-devel libuuid-devel openssl-devel docbook-style-xsl
编译安装,指定安装位置:
make prefix=/opt/software/mosquitto && make install
修改MOSQUITTO的配置文件
cd /etc/mosquitto
cp mosquitto.conf.example mosquitto.conf
vim mosquitto.conf
在Default listener 这里添加
port 1883
listener 9001
protocol websockets
添加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 的进程id,然后使用kill 命令指定进程id 即可停止mosquitto服务
#例如我这里的进程id是9307
ps -ef | grep mos
mosquitto -c /etc/mosquitto/mosquitto.conf -d
ss -tupln | grep 9001 #查看 9001 websocket 端口是否可以监听到
ss -tupln | grep 1883 #查看 1883 mqtt 端口是否可以监听到
关闭防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service
systemctl status firewalld.service
编写websocket客户端连接mosquitto服务并成功发布/订阅消息。
编译安装
创建软件安装目录:
mkdir mosquitto
进入解压后的文件夹:
cd v1.5.1
参考
https://blog.csdn.net/houjixin/article/details/46711547
https://blog.csdn.net/jasonsong2008/article/details/87874968
make[1]: Entering directory `/usr/software/mosquitto-1.5.1/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] Error 5
make[1]: Leaving directory `/usr/software/mosquitto-1.5.1/man'
make: *** [docs] Error 2
yum -y install docbook-style-xsl
查看docbook-style-xsl的安装位置
find / -name docbook.xsl
[root@localhost mosquitto]# mosquitto -c /etc/mosquitto/mosquitto.conf -d
mosquitto: error while loading shared libraries: libwebsockets.so.5: cannot open shared object file: No such file or directory
WITH_WEBSOCKETS:=yes
<script src="https://cdn.bootcdn.net/ajax/libs/mqtt/3.0.0/mqtt.min.js"></script>
<script>
let topic = "IOT_MQTT_TOPIC/message/michael"
let client = mqtt.connect('mqtt://192.168.100.11:9001', {
clientId: 'michael',
username: 'michael',
password: 'michael'
});
client.on('connect', function(res) {
console.log('mqtt连接成功...' + JSON.stringify(res))
client.subscribe(topic, function(err) {
if (!err) {
console.log('订阅成功', topic)
let i = 1
setInterval(() => {
client.publish(topic, 'hello mqtt' + i++)
}, 5000)
}
})
})
client.on('reconnect', function() {
console.log('mqtt重新连接中...')
})
client.on('close', function() {
console.log('mqtt连接中断...')
client.reconnect()
})
client.on('error', function(res) {
console.log('mqtt连接失败' + JSON.stringify(res))
client.reconnect()
})
client.on('message', function(topic, message) {
console.log(message.toString())
})
</script>