新的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./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:///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 |
|