paho的mqtt C语言库怎么设置订阅的qos
时间: 2023-07-04 11:04:00 浏览: 127
在paho的mqtt C语言库中,可以通过设置`struct mqtt_topic`结构体中的`qos`字段来设置订阅的qos。具体可以参考以下代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MQTTClient.h"
#define ADDRESS "tcp://localhost:1883"
#define CLIENTID "ExampleClientSub"
#define TOPIC "test/topic"
#define QOS 1
#define TIMEOUT 10000L
int main(int argc, char* argv[])
{
MQTTClient client;
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
MQTTClient_message* msg = NULL;
MQTTClient_deliveryToken token;
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(-1);
}
MQTTClient_subscribe(client, TOPIC, QOS);
while (1)
{
MQTTClient_message* msg;
MQTTClient_deliveryToken token;
int rc;
rc = MQTTClient_receive(client, &msg, TIMEOUT);
if (rc != MQTTCLIENT_SUCCESS)
{
continue;
}
printf("Received message: %.*s\n", msg->payloadlen, (char*)msg->payload);
MQTTClient_freeMessage(&msg);
MQTTClient_free(topicName);
}
MQTTClient_disconnect(client, 10000);
MQTTClient_destroy(&client);
return rc;
}
```
在上述代码中,通过调用`MQTTClient_subscribe`函数来订阅指定的主题,并通过`QOS`参数来设置订阅的qos。在这里,我们将qos设置为1。
阅读全文