mqtt服務(wù)器搭建
1.安裝mosquitto
# 安裝mosquitto
$brew install mosquitto
## 下面兩個(gè)是網(wǎng)上流傳最多的啟動(dòng)指令离熏,我個(gè)人沒(méi)用下面兩個(gè)指令
# 啟動(dòng)服務(wù)器
$brew services start mosquitto
# 停止服務(wù)
$brew services stop mosquitto
配置地址和端口:
image.png
$open /usr/local/etc/mosquitto/mosquitto.conf
# 如果無(wú)法正常打開(kāi)就只能自己手動(dòng)去選擇打開(kāi)方式,我直接設(shè)置了默認(rèn)打開(kāi)方式戴涝,這里第一次進(jìn)去可能看到的是一個(gè)快捷方式滋戳,你需要顯示原身再打開(kāi)
# 有些版本安裝的路徑不一定是上述的,例如在
/usr/local/Cellar/mosquitto/1.5.8/etc/mosquitto/mosquitto.conf\
這里需要具體情況具體分析
查看文件目錄:
image.png
# 設(shè)置路徑 打開(kāi) mosquitto.conf
password_file ../etc/mosquitto/pwfile.example
acl_file ../etc/mosquitto/aclfile.example
2.新建用戶(hù)
# 格式為
# username:password
# e.g:
avalanching1:123456
# 創(chuàng)建完畢了啥刻,需要在運(yùn)行mosquitto_passwd指令奸鸯,最好是在含有mosquitto_passwd執(zhí)行文件的bin文件夾中運(yùn)行
# 指令如下
$./mosquitto_passwd -U ../etc/mosquitto/pwfile.example
# 這里采用相對(duì)路徑,是相對(duì)于mosquitto_passwd這個(gè)執(zhí)行文件而言
image.png
3.設(shè)置用戶(hù)的權(quán)限
# 格式: user username topic read/write/readwrite toptic'name
# read 只讀
# write 只寫(xiě)
# readwrite 可讀可寫(xiě)
#e.g
user avalanching1 topic readwrite $SYS/IM
image.png
4.測(cè)試服務(wù)器
準(zhǔn)備如下終端:
image.png
輸入指令
# 開(kāi)啟服務(wù)器
$./mosquitto
# 登陸用戶(hù)avalanching1
$./mosquitto_sub -t $SYS/IM -I avalanching1
# 登陸用戶(hù)avalanching2
$./mosquitto_sub -t $SYS/IM -I avalanching2
# 以不同角色發(fā)送消息
$./mosquitto_pub -t $SYS/IM -i avalanching1 -m "hello world"
$./mosquitto_pub -t $SYS/IM -i avalanching2 -m "hello world"
$./mosquitto_pub -t $SYS/IM -i avalanching2 -m "hello world Avalanching1"
$./mosquitto_pub -t $SYS/IM -i avalanching1 -m "hello world Avalanching2"
# 關(guān)閉服務(wù)器
control + C
image.png
至此mosquitto服務(wù)器搭建完畢了