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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值