mqtt服務器搭建
1.安裝mosquitto
# 安裝mosquitto
$brew install mosquitto
## 下面兩個是網(wǎng)上流傳最多的啟動指令,我個人沒用下面兩個指令
# 啟動服務器
$brew services start mosquitto
# 停止服務
$brew services stop mosquitto
配置地址和端口:

image.png
$open /usr/local/etc/mosquitto/mosquitto.conf
# 如果無法正常打開就只能自己手動去選擇打開方式,我直接設置了默認打開方式,這里第一次進去可能看到的是一個快捷方式,你需要顯示原身再打開
# 有些版本安裝的路徑不一定是上述的,例如在
/usr/local/Cellar/mosquitto/1.5.8/etc/mosquitto/mosquitto.conf\
這里需要具體情況具體分析
查看文件目錄:

image.png
# 設置路徑 打開 mosquitto.conf
password_file ../etc/mosquitto/pwfile.example
acl_file ../etc/mosquitto/aclfile.example
2.新建用戶
# 格式為
# username:password
# e.g:
avalanching1:123456
# 創(chuàng)建完畢了,需要在運行mosquitto_passwd指令,最好是在含有mosquitto_passwd執(zhí)行文件的bin文件夾中運行
# 指令如下
$./mosquitto_passwd -U ../etc/mosquitto/pwfile.example
# 這里采用相對路徑,是相對于mosquitto_passwd這個執(zhí)行文件而言

image.png
3.設置用戶的權(quán)限
# 格式: user username topic read/write/readwrite toptic'name
# read 只讀
# write 只寫
# readwrite 可讀可寫
#e.g
user avalanching1 topic readwrite $SYS/IM

image.png
4.測試服務器
準備如下終端:

image.png
輸入指令
# 開啟服務器
$./mosquitto
# 登陸用戶avalanching1
$./mosquitto_sub -t $SYS/IM -I avalanching1
# 登陸用戶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"
# 關閉服務器
control + C

image.png
至此mosquitto服務器搭建完畢了