二、 Mosquitto 使用说明

一、 继上一篇文章<<Mosquitto 介绍&安装>> 之后、本章介绍 Mosquitto 的 简单使用。

1> 创建用户

 

# groupadd mosquitto
  # useradd -g mosquitto mosquitto

 

2> 程序配置

# mv /etc/mosquitto/mosquitto.conf.example /etc/mosquitto/mosquitto.conf

3> 配置说明

# 服务进程的PID
#pid_file /var/run/mosquitto.pid
 
# 服务进程的系统用户
#user mosquitto
 
# 服务绑定的IP地址
#bind_address
 
# 服务绑定的端口号
#port 1883
 
# 允许的最大连接数,-1表示没有限制
#max_connections -1
 
# 允许匿名用户
#allow_anonymous true

4> 如果实现websocket 功能 添加如下配置:

 

mosquitto.conf 的“Default Listener” 一节添加如下几行: 
port 1883 
listener 9001 
protocol websockets 

 

将websocket 端口映射到 9001 上 

后续需要使用libwebsockets的库文件,如果找不到库,执行下面的命令为库做一下符号连接(分64位和32位)

ln -s /usr/local/lib/libwebsockets.so.10 /usr/lib64/libwebsockets.so.10

5> 启动 mosquitto

# mosquitto -c /etc/mosquitto/mosquitto.conf –d

启动如出现如下错误:

 

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

 

 是libwebsockets 库的问题:

ln -s /usr/local/lib64/libwebsockets.so.4.0.0 /usr/lib/libwebsockets.so.4.0.0 

 还出现上述问题:

vi /etc/ld.so.conf.d/lib64c.conf 

…and add the following lines:- 
==lib64c default configuration 
/usr/local/lib64 
…and then:- 

 ldconfig

 成功启动1883端口监听

5> 测试:
5.1>  打开两个shell窗口
5.2> 订阅一个主题

# mosquitto_sub -t mqtt

5.3> 在另一窗口推送消息

# mosquitto_pub -t mqtt -h localhost -m "new location“

OK 这时会在订阅窗口看到推送的信息

new location

 

posted @ 2017-09-05 14:35  阅历笔记  阅读(7300)  评论(0编辑  收藏  举报