赞
踩
安装
- sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
- sudo apt-get update
- sudo apt-get install mosquitto
- sudo apt-get update
- sudo apt-get install mosquitto-dev
- sudo apt-get install mosquitto-clients
查看状态
sudo service mosquitto status
本地测试
- # 订阅
- mosquitto_sub -h localhost -t "/test/#"-v
-
- # 发布
- mosquitto_pub -h localhost -t "/test/t" -m "this is my first message"
- # 李雷只能发布以test为前缀的主题,订阅以$SYS开头的主题即系统主题
- user lilei
- topic write test/#
- topic read $SYS/#
-
- # 韩梅梅只能订阅以test为前缀的主题
- user hanmeimei
- topic read test/#
3.主配置文件,这是也是简单配置。其他见doc里的examples
- # Place your local configuration in /etc/mosquitto/conf.d/
- #
- # A full description of the configuration file is at
- # /usr/share/doc/mosquitto/examples/mosquitto.conf.example
-
- persistence true
- persistence_location /var/lib/mosquitto/
- log_dest file /var/log/mosquitto/mosquitto.log
- include_dir /etc/mosquitto/conf.d
-
- # jetaime 新增
- # 更换远程端口,默认为1883
- listener 2005
- # 允许匿名登录
- allow_anonymous false
- # 密码文件
- password_file /etc/mosquitto/pwfile.example
- # 用户访问控制策略
- acl_file /etc/mosquitto/acl
ps1:最后一行我的文件是acl,因为我配置就是acl.example而不是aclfile.exampe
ps2:云端部署的话,请一定注意防火墙设置,以防端口不通
这里,我们基础配置就结束了。那么用新配件的文件启动下
mosquitto -c /etc/mosquitto/mosquitto.conf -d
sub:
mosquitto_sub -h ip -p port -t topic -u username -P password
pub:
mosquitto_pub -h ip -p port -t topic -m message -u username -P password
这里测试,我写了个简单shell(1秒模拟发布一次)
- #!/bin/sh
- while [ true ];
- do
- timej=$(date "+%Y-%m-%d %H:%M:%S")
- echo $timej + ": send a message"
- mosquitto_pub -h *.*.*.* -p 2005 -t '/test/m/hzh' -m "$timej:this is a test message from u_wjy." -u 'username' -P 'userpass'
- sleep 1
- done
这样就结束,可以收到信息,当然mqtt可以配置的规则很多,包括绘画保持、离线接收等等请仔细看官文
链接:从别处贴一份关于sub,pub的参数说明https://blog.csdn.net/yangbingzhou/article/details/51324471
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。