• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Linux下安装Mosquitto以和开启Websockets

武飞扬头像
Quite不Quiet
帮助3

环境:Linux。

需求:安装Mosquitto服务,以及开启Mosquitto的Websockets服务。

安装包:Mosquitto1.6.9、libwebsockets

注意事项:1.先安装libwebsockets服务,且确保服务能够正常启动使用。

                  2.确保libwebsockets服务正常,再进行mosquitto服务的配置安装。


1. 安装libwebsockets

1.1 安装libwebsockets的依赖

依赖的安装,注意缺一不可,有yum仓库就用yum装,或者apt等仓库。

yum -y install openssl openssl-devel cmake 

1.2 安装libwebsockets

这里你可以选择直接git clone到本地解压,地址为:https://github.com/warmcat/libwebsockets

为方便下载,附上其他用户上传的libwebsockets的网盘地址:

libwebsockets.tar.gz - 蓝奏云

1.2.1 解压安装

  1.  
     sudo tar -zxvf libwebsockets.tar.gz -C /usr/local/libwebsockets
  2.  
     cd /usr/local/libwebsockets
  3.  
     mkdir build
  4.  
     cd build/
  5.  
     cmake ..
  6.  
     make && make install

如无报错情况下,build/bin目录下,将生成 example类文件。

1.2.2.二次编译

进入example的源码目录:libwebsockets/minimal-examples-lowlevel/ws-server/minimal-ws-server

进行编译,并启动服务进行测试。

  1.  
     cd /usr/local/libwebsockets/minimal-examples-lowlevel/ws-server/minimal-ws-server
  2.  
     cmake .
  3.  
     make
  4.  
     ./lws-minimal-ws-server

此时便可以通过,本机ip加7681端口,访问网页,查看服务启动状态。

即,若本机IP地址为 192.168.1.1,则可访问 192.168.1.1:7681 或 127.0.0.1:7681(本机访问)。

若安装成功:网页应正常显示出libwebsockets页面。

显示libwebsockets标识页面即代表编译安装成功,否则libwebsockkets将不可用。

否则,

需要检查,上述步骤是否在编译过程中报错,重新执行。


2. 安装Mosquitto

2.1 安装依赖&解压

依赖的安装,注意缺一不可,有yum仓库就用yum装,或者apt等仓库。

Mosquitto版本这里我选择1.6.9,新版本2.0后的配置可能在配置时有差别导致无法开启websockets服务。

附上传的 Mosquitto-1.6.9 的网盘地址:

mosquitto-1.6.9.tar.gz - 蓝奏云

  1.  
     yum -y install gcc gcc-c c-ares-devel uuid-devel libuuid-devel
  2.  
     
  3.  
     sudo tar -zxvf mosquitto-1.6.9.tar.gz -C /usr/local/

2.2 编辑 config.mk文件

  1.  
     cd /usr/local/mosquitto-1.6.9
  2.  
     vi config.mk

修改配置以开启websockets服务,于第68行,WITH_WEBSOCKETS:=no, 修改为yes

保存修改并退出。

  1.  
      67 # Build with websockets support on the broker.
  2.  
      68 WITH_WEBSOCKETS:=yes
  3.  
      69

2.3 执行安装

注意!当前路径仍为 /usr/local/mosquitto-1.6.9

  1.  
     adduser mosquitto
  2.  
     make && make install

2.4 配置 mosquitto.conf文件

此时路径为 /etc/mosquitto/

  1.  
     cd /etc/mosquitto/
  2.  
     cp mosquitto.conf.example mosquitto.conf
  3.  
     vi mosquitto.conf

修改位置为,第201行(Default listener)后的部分,主要是设置默认port端口以及websockets端口

这里于,第211~213行添加了配置,添加了默认端口1883,以及9001端口并指定为websockets,修改完成后,保存修改并退出。

  1.  
     200 # =================================================================
  2.  
     201 # Default listener
  3.  
     202 # =================================================================
  4.  
     203
  5.  
     204 # IP address/hostname to bind the default listener to. If not
  6.  
     205 # given, the default listener will not be bound to a specific
  7.  
     206 # address and so will be accessible to all network interfaces.
  8.  
     207 # bind_address ip-address/host name
  9.  
     208 #bind_address
  10.  
     209
  11.  
     210 # Port to use for the default listener.
  12.  
     211 port 1883
  13.  
     212 listener 9001
  14.  
     213 protocol websockets
  15.  
     214 # Bind the listener to a specific interface. This is similar to
  16.  
     215 # bind_address above but is useful when an interface has multiple addresses or
  17.  
     216 # the address may change. It is valid to use this with the bind_address option,
  18.  
     217 # but take care that the interface you are binding to contains the address you
  19.  
     218 # are binding to, otherwise you will not be able to connect.
  20.  
     219 # Example: bind_interface eth0
  21.  
     220 #bind_interface
学新通

2.5 运行测试

 mosquitto -v -c /etc/mosquitto/mosquitto.conf

如无特殊情况,mosquitto服务将正常启动,将按照指定路径配置的conf文件,开启1883以及9001端口。


2.6 关于Mosquitto服务启动时遇到的报错处理

在运行mosquitto命令,启动mosquitto服务时,可能会遇到报错,导致无法正常运行。

例1:找不到 libwebsockets.so.xx。

mosquitto: error while loading shared libraries: libwebsockets.so.19: cannot open shared object file: No such file or directory

原因:部分主机安装服务后,软连接没有建立。

解决办法:建立软连接。

  1.  
     sudo ln -s /usr/local/lib/libwebsocket.so.xx /usr/lib/libwebsocket.so.xx
  2.  
     ldconfig

例2:无效的用户 'mosquitto'。

 Error: Invalid user 'mosquitto'.

解决办法:新建用户。

add user mosquitto

例3:依赖问题。

error: ‘struct mosquitto’ has no member named ‘ssl’mosq->ssl = (SSL *)in

解决方法:此类皆是依赖没有提前安装好的问题,需提前安装好依赖,如openssl-devel。

例4:websockets 不可用。

Error: Websockets support not available.

原因:服务安装的顺序问题导致。

解决方法:需先正确安装libwebsockets后,在mosquitto的安装过程中,配置config.mk文件,启用包含websockets服务的安装。

以上就是Linux环境下,安装Mosquitto服务以及开启Websockets的全部步骤。


这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhehfjhg
系列文章
更多 icon
同类精品
更多 icon
继续加载