mqtt(2):mosquitto c 项目构建 mqtt broker 服务

前言


本文的原文连接是: https://blog.csdn.net/freewebsys/article/details/87606981
未经博主允许不得转载。
博主地址是:http://blog.csdn.net/freewebsys

1,关于 mosquitto


这个是一个 eclipse 项目下面的。
https://mosquitto.org/
github项目地址:
https://github.com/eclipse/mosquitto
docker hub 项目地址:
https://hub.docker.com/_/eclipse-mosquitto
dockerfile :
https://github.com/eclipse/mosquitto/blob/ff36baa49e37eb27d998314d46f01ddd914fdd65/docker/1.5/Dockerfile

非常的小呢。只有 不到 5MB 呢,c++开发就是省资源呢。

docker images
REPOSITORY                                 TAG                 IMAGE ID            CREATED             SIZE
eclipse-mosquitto                          latest              50373582ca5c        9 days ago          4.78MB

2,使用


使用docker file 启动:

docker run --name mosquitto  -itd -p 1883:1883 -p 9001:9001   eclipse-mosquitto:latest

因为系统是 alpine 打包的。所以没有 bash 使用 sh进行登录。

docker exec -it mosquitto sh
/ # ls
bin                   etc                   media                 proc                  sbin                  tmp
dev                   home                  mnt                   root                  srv                   usr
docker-entrypoint.sh  lib                   mosquitto             run                   sys                   var
/ # ps -ef
PID   USER     TIME  COMMAND
    1 mosquitt  0:00 /usr/sbin/mosquitto -c /mosquitto/config/mosquitto.conf
   11 root      0:00 sh
   18 root      0:00 ps -ef

然后使用 mqttbox 进行测试。


查看 docker 的log:发现新的连接connected 到服务器上了。

$ docker logs 51
1550463946: mosquitto version 1.5.6 starting
1550463946: Config loaded from /mosquitto/config/mosquitto.conf.
1550463946: Opening ipv4 listen socket on port 1883.
1550463946: Opening ipv6 listen socket on port 1883.
1550463981: New connection from 172.17.0.1 on port 1883.
1550463981: New client connected from 172.17.0.1 as 0a2da66f-5ead-44df-b5bf-646fa6b3529f1550463981657 (c1, k10).

3,总结


mosquitto 项目是 用 c++ 写的项目。
配置文件:
https://github.com/eclipse/mosquitto/blob/master/mosquitto.conf
c++写的非常的小。编译之后加上 alpine 也还是非常的小。
编译dockerfile 都写的详细了。

本文的原文连接是:
https://blog.csdn.net/freewebsys/article/details/87606981

博主地址是:http://blog.csdn.net/freewebsys

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mosquitto订阅端解析MQTT消息的JSON格式,你可以使用C语言中的标准库和第三方库来实现。以下是一个示例代码,演示了如何使用Mosquitto库订阅MQTT主题,并解析接收到的JSON消息: ```c #include <stdio.h> #include <stdlib.h> #include <mosquitto.h> #include <cJSON.h> #define MQTT_ADDRESS "mqtt.example.com" #define MQTT_PORT 1883 #define MQTT_TOPIC "sensors" // Mosquitto回调函数,用于处理连接成功、消息接收和连接断开时的事件 void on_connect(struct mosquitto *mosq, void *userdata, int result) { if(result == 0) { printf("Connected to MQTT broker\n"); mosquitto_subscribe(mosq, NULL, MQTT_TOPIC, 0); } else { fprintf(stderr, "Connect failed: %s\n", mosquitto_strerror(result)); } } void on_message(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *message) { if(message->payloadlen > 0) { // 解析JSON消息 cJSON *root = cJSON_Parse(message->payload); if(root == NULL) { fprintf(stderr, "Failed to parse JSON message\n"); return; } // 从JSON中提取数据 cJSON *file_content = cJSON_GetObjectItem(root, "file_content"); if(file_content != NULL && file_content->type == cJSON_String) { printf("Received file content: %s\n", file_content->valuestring); } // 释放JSON对象 cJSON_Delete(root); } } void on_disconnect(struct mosquitto *mosq, void *userdata, int rc) { printf("Disconnected from MQTT broker\n"); } int main() { // 初始化Mosquittomosquitto_lib_init(); // 创建Mosquitto客户端实例 struct mosquitto *mosq = mosquitto_new(NULL, true, NULL); if(mosq == NULL) { fprintf(stderr, "Failed to create Mosquitto instance\n"); mosquitto_lib_cleanup(); return 1; } // 设置连接、消息接收和连接断开的回调函数 mosquitto_connect_callback_set(mosq, on_connect); mosquitto_message_callback_set(mosq, on_message); mosquitto_disconnect_callback_set(mosq, on_disconnect); // 连接到MQTT代理服务器 int rc = mosquitto_connect(mosq, MQTT_ADDRESS, MQTT_PORT, 0); if(rc != MOSQ_ERR_SUCCESS) { fprintf(stderr, "Connect failed: %s\n", mosquitto_strerror(rc)); mosquitto_destroy(mosq); mosquitto_lib_cleanup(); return 1; } // 循环处理MQTT事件 while(mosquitto_loop(mosq, 0, 1) == MOSQ_ERR_SUCCESS) {} // 释放资源 mosquitto_destroy(mosq); mosquitto_lib_cleanup(); return 0; } ``` 在上述示例中,我们使用Mosquitto库订阅了一个MQTT主题,并设置了连接、消息接收和连接断开的回调函数。 在`on_message`回调函数中,我们首先将收到的MQTT消息的payload(即消息内容)解析为JSON对象。然后,我们可以使用cJSON库从JSON对象中提取所需的数据。在示例中,我们提取了名为`file_content`的字符串键值对。 最后,我们在`main`函数中循环处理MQTT事件,以保持与MQTT代理服务器的连接。当收到订阅的主题上的消息时,将触发`on_message`回调函数进行解析和处理。 请确保在编译和运行代码之前,你已正确安装了所需的Mosquitto库和cJSON库,并将相关的头文件和库文件包含到你的项目中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值