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
    评论
在 Windows C 语言程序中使用 MQTT 协议向 Mosquitto 服务器发送 JSON 数据,可以使用 Paho MQTT C 客户端库。下面是一个简单的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <MQTTClient.h> #include <cJSON.h> #define ADDRESS "tcp://10.10.10.97:1883" #define CLIENTID "ExampleClientPub" #define TOPIC "test" #define QOS 1 #define TIMEOUT 10000L int main(int argc, char* argv[]) { MQTTClient client; MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer; cJSON *root = NULL; char *payload = NULL; int rc; MQTTClient_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL); conn_opts.keepAliveInterval = 20; conn_opts.cleansession = 1; if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS) { printf("Failed to connect, return code %d\n", rc); exit(EXIT_FAILURE); } root = cJSON_CreateObject(); cJSON_AddStringToObject(root, "name", "John"); cJSON_AddStringToObject(root, "surname", "Doe"); cJSON_AddNumberToObject(root, "age", 30); payload = cJSON_Print(root); MQTTClient_message pubmsg = MQTTClient_message_initializer; pubmsg.payload = payload; pubmsg.payloadlen = strlen(payload); pubmsg.qos = QOS; pubmsg.retained = 0; MQTTClient_deliveryToken token; MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token); printf("Waiting for up to %d seconds for publication of %s\n" "on topic %s for client with ClientID: %s\n", (int)(TIMEOUT / 1000), payload, TOPIC, CLIENTID); rc = MQTTClient_waitForCompletion(client, token, TIMEOUT); printf("Message with delivery token %d delivered\n", token); cJSON_Delete(root); free(payload); MQTTClient_disconnect(client, 10000); MQTTClient_destroy(&client); return rc; } ``` 这个示例代码中,首先定义了一些常量,包括 MQTT 代理服务器的地址 `ADDRESS`、客户端 ID `CLIENTID`、MQTT 主题 `TOPIC`、消息 QoS 级别 `QOS`、和超时时间 `TIMEOUT`。 然后,在 `main` 函数中,创建了一个 MQTT 客户端实例 `client`。使用 `MQTTClient_connect` 函数连接 MQTT 代理服务器。连接成功后,使用 cJSON 库创建了一个 JSON 对象 `root`,并向其中添加了三个属性:`name`、`surname` 和 `age`。使用 `cJSON_Print` 函数将 JSON 对象转换成字符串格式,保存在变量 `payload` 中。 接着,定义了一个 MQTT 消息结构体 `pubmsg`,并设置了消息内容为 `payload`、消息长度为字符串长度、QoS 级别为 1,保留标志为 0。使用 `MQTTClient_publishMessage` 函数将消息发布到指定的 MQTT 主题上,并获取了发布令牌 `token`。 等待消息发布完成,使用 `MQTTClient_waitForCompletion` 函数等待消息传递完成,超时时间为 `TIMEOUT`。如果消息成功发送,则打印消息传递令牌 `token`。 最后,释放 JSON 对象内存、释放字符串内存、断开 MQTT 连接并销毁 MQTT 客户端实例,然后返回程序执行结果。 请注意,这个示例代码中的 MQTT 连接是不加密的,如果需要加密,请使用 SSL/TLS 协议连接 Mosquitto 服务器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值