Mosquitto安装配置websockets

本文档基于centos7部署

部署前准备:

安装wget命令 yum -y install wget

安装gcc-c++

yum install gcc-c++

安装cmake

yum install cmake

安装openssl-devel

yum install openssl-devel

安装libwebsockets

mkdir 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 ..

make install

创建软连接

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

ldconfig

安装mosquitto

mkdir mqtt

cd mqtt

wget http://mosquitto.org/files/source/mosquitto-1.6.3.tar.gz

tar -zxvf mosquitto-1.6.3.tar.gz

cd mosquitto-1.6.3

修改config.mk文件

vi config.mk

WITH_WEBSOCKETS:=yes

make

make install

创建软连接

ln -s /usr/local/lib/libmosquitto.so.1 /usr/lib/libmosquitto.so.1

ldconfig

进入/etc/mosquitto目录修改配置文件

复制mosquitto.conf.example配置文件

cp mosquitto.conf.example mosquitto.conf

cp pwfile.example pwfile.conf

cp aclfile.example aclfile.conf

vi mosquitto.conf

修改前 #allow_anonymous:true

修改后 allow_anonymous:false

修改前 #password_file

修改后 password_file  /etc/mosquitto/pwfile.conf

修改前 #acl_file

修改后 /etc/mosquitto/aclfile.conf

修改前 #protocol mqtt

修改后 protocol websockets

添加mosquitto 用户名密码

mosquitto_passwd /etc/mosquitto/pwfile.conf mqtttest 回车输入两次密码完成

修改用户权限

vi aclfile.conf

添加用户配置

user mqtttest

topic readwrite mqtttest (此处mqtttest为发布\订阅的标识符,读写权限read/write/readwrite)

创建用户测试

groupadd mosquitto

useradd -g mosquitto mosquitto

启动命令

mosquitto -c /etc/mosquitto/mosquitto.conf

打开外部访问端口

--permanent永久有效否则重启失效

firewall-cmd --zone=public --add-port=1883/tcp --permanent

添加端口后重新载入

firewall-cmd --reload

浏览器客户端代码示例

<!DOCTYPE html>

<html>

         <head>

                   <meta charset="utf-8">

                   <title></title>

                   <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.2/mqttws31.js"></script>

         </head>

         <body>

                   <script type="text/javascript">

                            var mqtt;

                            var host = '192.168.91.130';

                            var port = 1883;

                            // onConnect 事件

                            function onConnect() {

                                     console.log('connected.');

                                     let i=0;

                                     let timer = setInterval(() => {

                                               i++;

                                               var raw_message = '信息!'+i;

                                               message = new Paho.MQTT.Message(raw_message);

                                               message.destinationName = 'mqtttest';

                                               console.log('sending message: ' + raw_message);

                                               mqtt.send(message);

                                               if(i >= 5){

                                                        clearInterval(timer);

                                               }

                                     },1000)

                                     // 订阅 download topic

                                     var subOptions = {

                                               qos: 1,

                                               onSuccess: onSubscribe

                                     };

                                     mqtt.subscribe('mqtttest', subOptions);

                            }

                            // 订阅主题成功事件

                            function onSubscribe(context) {

                                     console.log('subscribe success');

                                     console.log(context);

                            }

                            // 连接失败事件

                            function onFailure(message) {

                                     console.log('connect failed.');

                            }

                            // onMessageArrived 事件

                            function onMessageArrived(message) {

                                     console.log('new message arrived...');

                                     console.log(message.payloadString);

                            }

                            // 建立 MQTT websocket 连接

                            function MQTTconnect() {

                                     console.log('connecting to ' + host + ':' + port);

                                     mqtt = new Paho.MQTT.Client(host, port, 'clientid');

                                     var options = {

                                               timeout: 3,

                                               onSuccess: onConnect,

                                               onFailure: onFailure,

                                               userName: 'mqtttest',

                                               password: 'mqtttest',

                                               mqttVersion: 4

                                     };

                                     mqtt.onMessageArrived = onMessageArrived;

                                     mqtt.connect(options);

                            }

                            MQTTconnect();

                   </script>

         </body>

</html>

上一篇:powershell语言特性(免杀方法)


下一篇:Ansible:服务器巡检_3、Windows 平台巡检