赞
踩
1.下载依赖库
输入命令:
sudo apt-get install libssl-dev
sudo apt-get install uuid-dev
sudo apt-get install cmake
2.下载mosquitto源码
2.1第一步:打开链接 Index of /files/source/ ,定位到mosquitto-1.6.9.tar.gz,下载到虚拟机与主机共享文件夹中。
2.2第二步:解压。
输入命令:
tar -zxvf mosquitto-1.6.9.tar.gz -C /home
3. 编译与安装
输入命令:
cd /home/mosquitto-1.6.9
make
输入命令:
sudo make install
输入命令:
sudo service mosquitto status
sudo service mosquitto start
sudo service mosquitto stop
4.配置
安装完成后, 配置文件都在/etc/mosquitto目录下
主配置文件mosquitto.conf
# 不允许匿名
allow_anonymous false
# 配置用户密码文件
password_file /etc/mosquitto/pwfile
# 配置topic和用户
acl_file /etc/mosquitto/acl
sudo mosquitto_passwd -c /etc/mosquitto/pwdfile admin
sudo mosquitto_passwd /etc/mosquitto/pwdfile admin
命令解释:
第一个命令,参数多一个 -c,是创建 /etc/mosquito/passwd文件,将用户名和密码写到文件中,用户名是明文,密码是加密过的密文。
第二个命令,是在已有的用户上添加新的用户(注意第二次创建用户时不用加 -c 如果加 -c 会把第一次创建的用户覆盖)。
配置好后,代理会屏蔽匿名用户。
输入命令:
sudo vim /etc/mosquitto/acl
添加以下内容:
user stonegeek stonegeek是前面已添加的用户名
topic write mtopic/# mtopic是指定的主题
user stonegeek
topic read mtopic/#
解释:
user后面添加已有用户名,topic 后面添加可读或者可写的主题,read 是可读,write是可写。
配置完成后重启。
echo "关闭程序"
kill -9 $(pidof mosquitto)
echo "运行程序"
mosquitto -c /etc/mosquitto/mosquitto.conf -d
5.启动mosquitto
启动程序,输入命令:
mosquitto -c /etc/mosquitto/mosquitto.conf -d
再打开一个服务器窗口,在一个(订阅)窗口输入:
mosquitto_sub -h localhost -t mtopic -u stonegeek -P 123456
命令格式:
mosquitto_sub -h [IP] -t [topic] -u [user] -P [password]
参数介绍:
-h | 服务器主机,默认localhost |
-t | 指定主题 |
-u | 用户名 |
-P | 密码 |
-i | 唯一的客户端ID |
另一个(发布)窗口输入:
mosquitto_pub -h localhost -t mtopic -u stonegeek -P 123456 -m “Hello world!”
命令格式:
mosquitto_pub -h [IP] -t [topic] -u [user] -P [password] -p [port] -m [message]
参数介绍:
-h | 服务器主机,默认localhost |
-t | 指定主题 |
-u | 用户名 |
-P | 密码 |
-i | 唯一的客户端ID |
-m | 发布的消息内容 |
这样,我们就成功订阅了主题为hello的消息了。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。