libpaho MQTT 客户端库的 MQTTClient_subscribe 函数进行订阅失败,返回错误码 128

解决方案一:

首先看下 128 代表什么:

/**
 * Bad return code from subscribe, as defined in the 3.1.1 specification
 */
#define MQTT_BAD_SUBSCRIBE 0x80

这个返回错误码 0x80 就是十进制的128,在 MQTT 3.1.1 版本中定义的,所以,我们可以优先考虑 MQTT broker 服务端协商的版本是否准确,

    // 设置连接选项
    conn_opts.keepAliveInterval = 20;
    conn_opts.cleansession = 1;
    conn_opts.username = MQTT_USERNAME;
    conn_opts.password = MQTT_PASSWORD;
    conn_opts.MQTTVersion = 3; //设置正确的版本

设置正确的MQTT版本

解决方案二:

1. MQTTCLIENT_FAILURE (128):通常表示订阅操作未成功完成,可能是由于连接问题、订阅主题不存在、订阅质量等级不支持、订阅超时或订阅限制等原因。需要进一步检查和排除这些问题。

2. MQTTCLIENT_DISCONNECTED (32100):表示与 MQTT 代理的连接已断开。在订阅之前,确保已成功连接到 MQTT 代理,否则订阅操作将无法执行。

3. MQTTCLIENT_BAD_ARGUMENT (4):表示传递给订阅函数的参数无效。检查订阅函数调用中传递的参数,确保它们是有效的。

4. MQTTCLIENT_MAX_MESSAGES_INFLIGHT (32202):表示达到了同时处理的最大传输消息数的限制。这可能是由于发布消息太频繁或设置了较小的最大传输消息数限制。可以尝试减少发布消息的频率或增加最大传输消息数的限制。

5. 其他错误码:libpaho MQTT 客户端库还提供了其他一些错误码,如认证失败、网络错误、订阅主题无效等。根据具体的错误码进行进一步的故障排除和问题解决。

附 MQTTClient.h 文件宏定义:

/**
 * Return code: No error. Indicates successful completion of an MQTT client
 * operation.
 */
#define MQTTCLIENT_SUCCESS 0
/**
 * Return code: A generic error code indicating the failure of an MQTT client
 * operation.
 */
#define MQTTCLIENT_FAILURE -1

/* error code -2 is MQTTCLIENT_PERSISTENCE_ERROR */

/**
 * Return code: The client is disconnected.
 */
#define MQTTCLIENT_DISCONNECTED -3
/**
 * Return code: The maximum number of messages allowed to be simultaneously
 * in-flight has been reached.
 */
#define MQTTCLIENT_MAX_MESSAGES_INFLIGHT -4
/**
 * Return code: An invalid UTF-8 string has been detected.
 */
#define MQTTCLIENT_BAD_UTF8_STRING -5
/**
 * Return code: A NULL parameter has been supplied when this is invalid.
 */
#define MQTTCLIENT_NULL_PARAMETER -6
/**
 * Return code: The topic has been truncated (the topic string includes
 * embedded NULL characters). String functions will not access the full topic.
 * Use the topic length value to access the full topic.
 */
#define MQTTCLIENT_TOPICNAME_TRUNCATED -7
/**
 * Return code: A structure parameter does not have the correct eyecatcher
 * and version number.
 */
#define MQTTCLIENT_BAD_STRUCTURE -8
/**
 * Return code: A QoS value that falls outside of the acceptable range (0,1,2)
 */
#define MQTTCLIENT_BAD_QOS -9
/**
 * Return code: Attempting SSL connection using non-SSL version of library
 */
#define MQTTCLIENT_SSL_NOT_SUPPORTED -10
 /**
  * Return code: unrecognized MQTT version
  */
 #define MQTTCLIENT_BAD_MQTT_VERSION -11
/**
 * Return code: protocol prefix in serverURI should be tcp:// or ssl://
 */
#define MQTTCLIENT_BAD_PROTOCOL -14
 /**
  * Return code: option not applicable to the requested version of MQTT
  */
 #define MQTTCLIENT_BAD_MQTT_OPTION -15
 /**
  * Return code: call not applicable to the requested version of MQTT
  */
 #define MQTTCLIENT_WRONG_MQTT_VERSION -16


/**
 * Default MQTT version to connect with.  Use 3.1.1 then fall back to 3.1
 */
#define MQTTVERSION_DEFAULT 0
/**
 * MQTT version to connect with: 3.1
 */
#define MQTTVERSION_3_1 3
/**
 * MQTT version to connect with: 3.1.1
 */
#define MQTTVERSION_3_1_1 4
 /**
  * MQTT version to connect with: 5
  */
 #define MQTTVERSION_5 5
/**
 * Bad return code from subscribe, as defined in the 3.1.1 specification
 */
#define MQTT_BAD_SUBSCRIBE 0x80

订阅端示例代码:demo.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MQTTClient.h"

#define MQTT_SERVER "127.0.0.1"
#define MQTT_PORT 1883
#define MQTT_TOPIC "test/topic"
#define MQTT_USERNAME "your_username"
#define MQTT_PASSWORD "your_password"
#define MQTT_CLIENTID "12345678"

// 定义消息接收回调函数
int messageArrived(void *context, char *topicName, int topicLen, MQTTClient_message *message) {
    printf("Received message: %.*s\n", message->payloadlen, (char *)message->payload);
    MQTTClient_freeMessage(&message);
    MQTTClient_free(topicName);
    return 1;
}

int main() {
    MQTTClient client;
    MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
    int rc;

    // 创建MQTT客户端实例
    MQTTClient_create(&client, MQTT_SERVER, MQTT_CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL);

    // 设置消息接收回调函数
    MQTTClient_setCallbacks(client, NULL, NULL, messageArrived, NULL);

    // 设置连接选项
    conn_opts.keepAliveInterval = 20;
    conn_opts.cleansession = 1;
    conn_opts.username = MQTT_USERNAME;
    conn_opts.password = MQTT_PASSWORD;
    conn_opts.MQTTVersion = 4; //

    // 连接MQTT代理服务器
    if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS) {
        printf("Failed to connect, return code %d\n", rc);
        exit(EXIT_FAILURE);
    }

    // 订阅主题并打印订阅端返回值
    int sub_qos = 1;
    int ret = MQTTClient_subscribe(client, MQTT_TOPIC, sub_qos);
    printf("MQTTClient_subscribe ret %d\n", ret);

    // 循环等待接收消息
    while (1) {
        MQTTClient_yield();
    }

    // 断开连接并清理资源
    MQTTClient_disconnect(client, 1000);
    MQTTClient_destroy(&client);

    return 0;
}

运行结果:

发布端:

~$ mosquitto_pub -h 127.0.0.1 -p 1883  -t test/topic -m 123

订阅端:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值