MQTT学习笔记-让Mosquitto支持Websocket

       新的Mosquitto版本1.4.2已经支持WebSocket,这就为我们HTML5客户端使用MQTT提供了方法,但是MQTT连接默认不支持WebSocket连接,所以我们要首先设置让Mosquitto支持WebSocket:
(如果遇见有的步骤不能执行或者除错,请使用管理员权限sudo执行)废话不讲,开始:

第一步:安装前准备用到的依赖包:
$ sudo apt-get update
$ sudo apt-get install build-essential python quilt devscripts python-setuptools python3
$ sudo apt-get install libssl-dev
$ sudo apt-get install cmake
$ sudo apt-get install libc-ares-dev
$ sudo apt-get install  uuid-dev
$ sudo apt-get install daemon
第二步:下载并编译安装 libwebsockets
$ wget  http://git.libwebsockets.org/cgi ... 3-firefox-36.tar.gz
$ tar zxvf libwebsockets-1.4-chrome43-firefox-36.tar.gz
$ cd libwebsockets-1.4-chrome43-firefox-36
$ mkdir build
$ cd build
$ cmake ..
$ make install
$ ldconfig
$ cd

第三步:下载并编译安装最新版 mosquitto 1.4.2

$ wget  http://mosquitto.org/files/source/mosquitto-1.4.2.tar.gz
$ tar zxvf mosquitto-1.4.2.tar.gz
$ cd mosquitto-1.4.2
更改configure.mk中
WITH_WEBSOCKETS:=no
变成(这一步是做WebSocket支持)

WITH_WEBSOCKETS:=yes
$ make
$ make install
$ cp mosquitto.conf /etc/mosquitto
第四步:配置Mosquitto能够使用WebSocket

请在/etc/mosquitto/mosquitto.conf 的“Default Listener”  一节添加如下几行:
port 1883
listener 9001
protocol websockets
你添加过后此文件像这样的:
# =================================================================


# Default listener
# =================================================================
# IP address/hostname to bind the default listener to. If not
# given, the default listener will not be bound to a specific
# address and so will be accessible to all network interfaces.
# bind_address ip-address/host name
#bind_address
# Port to use for the default listener.
port 1883
listener 9001
protocol websockets
第五步:添加Mosquitto用户:


$ adduser mosquitto
$ reboot
第六步:运行Mosquitto:

$ mosquitto -c /etc/mosquitto/mosquitto.conf
现在你可以试试使用Websocket客户端来连接你的MQTT服务器的9001端口!!!!!!!

——————————本文章由damoyelang1992在开源智能家居论坛首发,转载请注明链接,并mail——iqinfei@163.com————


注意:

如果提示uuid/uuid.h: 没有那个文件或目录,则需要使用命令sudo apt-get install uuid-dev安装uuid


  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值