From e2dcf2e20cfc080ea93b351696b20e33209e0cb6 Mon Sep 17 00:00:00 2001 From: flykhan Date: Thu, 26 Oct 2023 19:38:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9pubtomqtt=E7=9A=84=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E8=AF=AD=E5=8F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- how_to_use_mqtt/pub1.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/how_to_use_mqtt/pub1.c b/how_to_use_mqtt/pub1.c index 60dbcb0..47ba790 100644 --- a/how_to_use_mqtt/pub1.c +++ b/how_to_use_mqtt/pub1.c @@ -4,29 +4,35 @@ #include #include int isConnected = 0; -void connect_callback(struct mosquitto* mosq, void* obj, int rc) { - if (rc == 0) { +void connect_callback(struct mosquitto *mosq, void *obj, int rc) +{ + if (rc == 0) + { printf("---连接服务器成功---\n"); isConnected = 1; } } -int main(int argc, char const* argv[]) { - if (argc < 3) { +int main(int argc, char const *argv[]) +{ + if (argc < 3) + { printf("format: ./pub1 msgid topic msg"); return -1; } mosquitto_lib_init(); - struct mosquitto* client = mosquitto_new(NULL, true, NULL); + struct mosquitto *client = mosquitto_new(NULL, true, NULL); mosquitto_connect_callback_set(client, connect_callback); mosquitto_connect(client, "10.12.156.19", 1883, 60); mosquitto_loop_start(client); while (isConnected == 0) - int msg_id = atoi(argv[1]); - char* topic = (char*)argv[2]; - char* msg = (char*)argv[3]; + ; + int msg_id = atoi(argv[1]); + char *topic = (char *)argv[2]; + char *msg = (char *)argv[3]; int flag = mosquitto_publish(client, &msg_id, topic, strlen(msg), msg, 1, true); - if (flag == MOSQ_ERR_SUCCESS) { + if (flag == MOSQ_ERR_SUCCESS) + { printf("---消息发布成功: %s(%d):%s---\n", topic, msg_id, msg); mosquitto_destroy(client); mosquitto_lib_cleanup();