mosquitto支持websocket的使用方法

欢迎加入QQ群:221779856,国内最活跃的Mosquitto沟通社区,关于MQTT、Mosquitto、IM、推送系统、物联网、高并发处理等技术。

 

mosquito的版本要大于1.4.2;

 

1.修改配置:

打开configure.mk中的WITH_WEBSOCKETS配置项,如下:

WITH_WEBSOCKETS:=yes

2.编译

make

 

3.安装websocket:

(1)下载源码:

https://github.com/warmcat/libwebsockets

(2)拷贝到编译mosquitto的主机上:

1)进入到下载的源码中:/home/jason/tools/libwebsockets-master

2)创建一个build文件夹

3)进入到build目录中;

4)执行cmke,使用命令:cmake ..

5)make && make install

mkdir buildcd buildcmake ..makemake install

 

4.常见问题:

(1)如果遇到:

[root@iZbp1i90l0ap2clvybw1gdZ src]# ./mosquitto

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

1)先在目录/usr/local/lib看下libwebsockets.so的位置,这里的后缀数字名可能会不一致,如下所示:

[root@iZbp1i90l0ap2clvybw1gdZ lib]# pwd

/usr/local/lib

[root@iZbp1i90l0ap2clvybw1gdZ lib]# ls

cmake libwebsockets.a libwebsockets.so libwebsockets.so.12 pkgconfig

2)建立ibwebsockets.so.12的软连接;

sudo ln -s /usr/local/lib/libwebsockets.so.12 /usr/lib/libwebsockets.so.12

3)执行:

ldconfig

 

(2)如果遇到问题

websockets.c:205:10: error: ‘struct mosquitto’ has no member named ‘ssl’

mosq->ssl = (SSL *)in;

解决办法,说明没有安装SSL,可以选择

安装ssl:

yum install openssl

yum install openssl-devel

(3)问题:Error: Websockets support not available.

[root@iZm5e92vqf4urefs01djnxZ mosquitto-1.4.15]# ./src/mosquitto -c mosquitto.conf

Error: Websockets support not available.

Error found at mosquitto.conf:297.

Error: Unable to open configuration file.

原因:编译mosquitto的时候没有打开websocket功能,但是配置文件中却把websocket打开了;

编译mosquitto的时候打开websocket功能需要将config.mk中的配置项WITH_WEBSOCKETS设置为yes;

 

5.配置mosquitto.conf

特别注意,mosquitto支持多端口监听,而且不同的端口支持不同的协议,这样部署一个mosquitto程序就可以同时支持mqtt协议和websocket协议,mosquitto默认的监听端口号是1883,在Default listener配置段中配置,配置项是:port;在不使用扩展配置项的时候也不启用任何自定义端口配置的时候,mosquitto默认使用1883端口,但是一旦用户启用了扩展监听端口就必须显示指出默认端口号,即:Default listener配置段的port参数:

 

# =================================================================

# Default listener

# =================================================================

 

# Port to use for the default listener.

#port 1883

mosquitto使用扩展监听端口来支持websocket,即它本身通过默认监听端口号(port参数指定)来接收mqtt协议的连接请求,同时启用另外一个监听端口号(listener参数指定)来接收websocket协议的连接请求,其配置如下(扩展监听端口的相关信息都是在Extra listeners配置段之后),启用扩展端口号需要在Extra listeners配置段之下,显示指出扩展监听端口的端口号listener以及所支持的应用层协议类型:

# =================================================================

# Extra listeners

# =================================================================

# Listen on a port/ip address combination. By using this variable

# multiple times, mosquitto can listen on more than one port. If

# this variable is used and neither bind_address nor port given,

# then the default listener will not be started.

# The port number to listen on must be given. Optionally, an ip

# address or host name may be supplied as a second argument. In

# this case, mosquitto will attempt to bind the listener to that

# address and so restrict access to the associated network and

# interface. By default, mosquitto will listen on all interfaces.

# Note that for a websockets listener it is not possible to bind to a host

# name.

# listener port-number [ip address/host name]

listener 9001

# Choose the protocol to use when listening.

# This can be either mqtt or websockets.

# Certificate based TLS may be used with websockets, except that only the

# cafile, certfile, keyfile and ciphers options are supported.

protocol websockets

你可能感兴趣的:(计算机网络,MQTT协议及其应用,计算机网络)