iOS mqtt & protobuf(一) mosquitto服务器搭建

mqtt服务器搭建

1.安装mosquitto

# 安装mosquitto
$brew install mosquitto

## 下面两个是网上流传最多的启动指令,我个人没用下面两个指令
# 启动服务器
$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

# 创建完毕了,需要在运行mosquitto_passwd指令,最好是在含有mosquitto_passwd执行文件的bin文件夹中运行
# 指令如下
$./mosquitto_passwd -U ../etc/mosquitto/pwfile.example
# 这里采用相对路径,是相对于mosquitto_passwd这个执行文件而言
image.png

3.设置用户的权限

# 格式: 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
# 以不同角色发送消息

$./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服务器搭建完毕了

你可能感兴趣的:(iOS mqtt & protobuf(一) mosquitto服务器搭建)