• mosquitoo + websocket部署测试


    简述

    一款实现了消息推送协议 MQTT v3.1 的开源消息代理软件,提供轻量级的,支持可发布/可订阅的的消息推送模式,使设备对设备之间的短消息通信变得简单,比如现在应用广泛的低功耗传感器,手机、嵌入式计算机、微型控制器等移动设备。一个典型的应用案例就是 Andy Stanford-ClarkMosquitto(MQTT协议创始人之一)在家中实现的远程监控和自动化

    mosquitto官网  http://mosquitto.org/

    关于mqtt协议可参考 http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html

         yum安装       

    加源
    
    centos6
    
    [home_oojah_mqtt]
    
    name=mqtt (CentOS_CentOS-6)
    
    type=rpm-md
    
    baseurl=http://download.opensuse.org/repositories/home:/oojah:/mqtt/CentOS_CentOS-6/
    
    gpgcheck=1
    
    gpgkey=http://download.opensuse.org/repositories/home:/oojah:/mqtt/CentOS_CentOS-6/repodata/repomd.xml.key
    
    enabled=1
    
    centos7
    
    [home_oojah_mqtt]
    
    name=mqtt (CentOS_CentOS-7)
    
    type=rpm-md
    
    baseurl=http://download.opensuse.org/repositories/home:/oojah:/mqtt/CentOS_CentOS-7/
    
    gpgcheck=1
    
    gpgkey=http://download.opensuse.org/repositories/home:/oojah:/mqtt/CentOS_CentOS-7/repodata/repomd.xml.key
    
    enabled=1
    
     
    
    yum list mosquit*
    
    yum 安装需要的包
    View Code

    编译安装

    安装依赖

    yum install gcc gcc-c++ libstdc++-devel openssl-devel c-ares-devel  c-ares-devel uuid-devel  libuuid-devel cmake

    下载安装libwebsockets

    libwebsockets是一款轻量级用来开发服务器和客户端的C库。按照官方(https://libwebsockets.org/)给出的介绍来看,它不仅支持ws,wss还同时支持http与https,可以轻轻松松结合openssl等库来实现ssl加密

    [root@server6 mosquitto]# git clone https://github.com/warmcat/libwebsockets.git
    [root@server6 mosquitto]# cd libwebsockets/
    [root@server6 libwebsockets]# mkdir build
    [root@server6 libwebsockets]# cd build
    [root@server6 build]# cmake ..
    [root@server6 build]# make install
    View Code

    cd /opt/mqtt

    wget https://github.com/warmcat/libwebsockets/archive/v1.3-chrome37-firefox30.tar.gz

    tar xf v1.3-chrome37-firefox30.tar.gz

    cd libwebsockets-1.3-chrome37-firefox30/

    mkdir build

    cd build

    cmake ..   -DLIB_SUFFIX=64

    make install

    ldconfig

    什么是Websockets及其工作原理?

    WebSocket是一种计算机通信协议,通过单个TCP / IP连接提供全双工通信通道。维基

    它与http密切相关,因为它使用http进行初始连接建立。

    客户端和服务器使用http进行连接,然后协商连接升级到websockets,然后连接从http 切换到websockets。

    客户端和服务器现在可以通过连接交换全双工二进制数据

     

    下载安装 mosquitto

    cd /opt/mqtt/

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

    tar xf mosquitto-1.4.2.tar.gz

    cd mosquitto-1.4.2

    sed -i 's#WITH_WEBSOCKETS:=no#WITH_WEBSOCKETS:=yes#g' config.mk

    make

    make install

    cp mosquitto.conf /etc/mosquitto/

    sed -i '/#port 1883/a port 1883 listener 9001 protocol websockets' /etc/mosquitto/mosquitto.conf

    启动 -d

    创建用户   useradd mosquitto

    [root@ecs-2018-0002 mosquitto-1.4.14]# mosquitto -c /etc/mosquitto/mosquitto.conf Error: Unable to open include_dir '/etc/mosquitto/conf.d'. Error found at /etc/mosquitto/mosquitto.conf:10. Error: Unable to open configuration file. [root@ecs-2018-0002 mosquitto-1.4.14]# ll /etc/mosquitto/conf.d ls: cannot access /etc/mosquitto/conf.d: No such file or directory [root@ecs-2018-0002 mosquitto-1.4.14]# mkdir -p /etc/mosquitto/conf.d [root@ecs-2018-0002 mosquitto-1.4.14]# mosquitto -c /etc/mosquitto/mosquitto.conf 1539235869: mosquitto version 1.4.14 (build date 2018-10-11 13:25:17+0800) starting 1539235869: Config loaded from /etc/mosquitto/mosquitto.conf. 1539235869: Opening ipv4 listen socket on port 1883. 1539235869: Opening ipv6 listen socket on port 1883. ^C1539235887: mosquitto version 1.4.14 terminating 1539235887: Saving in-memory database to /var/lib/mosquitto/mosquitto.db. 1539235887: Error saving in-memory database, unable to open /var/lib/mosquitto/mosquitto.db.new for writing. 1539235887: Error: No such file or directory. [root@ecs-2018-0002 mosquitto-1.4.14]# mosquitto -c /etc/mosquitto/mosquitto.conf -d
    [root@ecs-2018-0002 mosquitto-1.4.14]# netstat -lntp|grep 1883
    tcp 0 0 0.0.0.0:1883 0.0.0.0:* LISTEN 974/mosquitto
    tcp 0 0 :::1883 :::* LISTEN 974/mosquitto

    问题报错

    [root@admin mosquitto-1.4.2]# mosquitto -c /etc/mosquitto/mosquitto.conf
    mosquitto: error while loading shared libraries: libwebsockets.so.4.0.0: cannot open shared object file: No such file or directory

    添加库目录

    vim /etc/ld.so.conf.d/liblocal.conf

    /usr/local/lib64

    /usr/local/lib

    刷新链接库

    ldconfig

    [root@local wutest]# cat install_mqtt.sh 
    yum install gcc gcc-c++ libstdc++-devel openssl-devel c-ares-devel  c-ares-devel uuid-devel  libuuid-devel cmake -y
    mkdir -p  /opt/mqtt
    cd /opt/mqtt
    wget https://github.com/warmcat/libwebsockets/archive/v1.3-chrome37-firefox30.tar.gz
    tar xf v1.3-chrome37-firefox30.tar.gz
    cd libwebsockets-1.3-chrome37-firefox30/
    mkdir build
    cd build
    cmake .. -DLIB_SUFFIX=64
    make install
    cat >>/etc/ld.so.conf.d/liblocal.conf<<eop
    /usr/local/lib64
    /usr/local/lib
    eop
    ldconfig
    cd /opt/mqtt/
    wget http://mosquitto.org/files/source/mosquitto-1.4.2.tar.gz
    tar xf mosquitto-1.4.2.tar.gz
    cd mosquitto-1.4.2
    sed -i 's#WITH_WEBSOCKETS:=no#WITH_WEBSOCKETS:=yes#g' config.mk
    make
    make install
    cp mosquitto.conf /etc/mosquitto/
    sed -i '/#port 1883/a port 1883
    listener 9001
    protocol websockets' /etc/mosquitto/mosquitto.conf
    useradd mosquitto
    ldconfig
    shell

    测试

    1 启动代理服务

    mosquitto -v

        【-v】打印更多的调试信息

    2 订阅主题

    mosquitto_sub -v -t sensor

        【-t】指定主题,此处为sensor

        【-v】打印更多的调试信息

    3 发布内容

    mosquitto_pub -t sensor  -m 666

        【-t】指定主题

        【-m】指定消息内容

    4 运行结果

        当发布者推送消息之后,订阅者获得以下内容

    sensor 666

        而代理服务器控制台中会出现——连接、消息发布和心跳等调试信息。通过代理服务器的调试输出可以了解MQTT协议的相关过程

    http://goochgooch.co.uk/2014/08/01/building-mosquitto-1-4/

    http://www.steves-internet-guide.com/mqtt-websockets/

    https://blog.csdn.net/u013332124/article/details/79480639

    自用博文.不限传播.欢迎留言.共同进步.
  • 相关阅读:
    在window10系统下安装redis
    Dubbo 基础用法
    vue处理对象值改变时dom值没有跟着变化的问题
    KMP算法
    Leetcode 502 IPO
    vue页面无法正常渲染的问题
    vue锚点双向绑定
    JavaScript克隆一个对象
    el-table刷新后table出现抖动现象
    el-table组件去掉滚动条的问题
  • 原文地址:https://www.cnblogs.com/mingetty/p/8135303.html
Copyright © 2020-2023  润新知