on_connect 回调持续调用

问题描述 投票:0回答:1

我有一个简单的 C++ 应用程序在与 Mosquitto 代理相同的本地运行。我正在对我的应用程序进行一些调试,我注意到 on_connect 回调不断触发,并且代理不断显示以下两行。

New Client connected from 10.0.2.37 as publisher-test
Client publisher-test already connected, closing old connection 

这看起来很奇怪,我想弄清楚如何阻止它发生。

这是我设置 mqtt 客户端并启动与代理的连接的代码。

bool MosquittoAppServer::InitMosquitto() {
    int rc = mosquitto_lib_init();
    printf("MosquittoAppServer::InitMosquitto Breeakpoint #1 - mosquitto_lib_init %s \r\n", rc==MOSQ_ERR_SUCCESS ? "succeeded" : "failed");
    mMosq = mosquitto_new("publisher-test", true, NULL);
    printf("MosquittoAppServer::InitMosquitto Breeakpoint #2A %x %s \r\n",&mMosq, mMosq == NULL ? "failed to create mosquitto obj": "successfully created mosquitto obj");
    mosquitto_connect_callback_set(mMosq, on_connect_callback);
    printf("MosquittoAppServer::InitMosquitto Breeakpoint #2B mosquitto_connect_callback_set \r\n");
    mosquitto_publish_callback_set(mMosq, on_pub_callback);
    printf("MosquittoAppServer::InitMosquitto Breeakpoint #2C mosquitto_publish_callback_set \r\n");
    mosquitto_subscribe_callback_set(mMosq, on_sub_callback);
    printf("MosquittoAppServer::InitMosquitto Breeakpoint #2D mosquitto_subscribe_callback_set \r\n");
    mosquitto_message_callback_set(mMosq, on_msg_callback);
    printf("MosquittoAppServer::InitMosquitto Breeakpoint #2E mosquitto_message_callback_set \r\n");
    mosquitto_user_data_set(mMosq,  this);
    printf("MosquittoAppServers::InitMosquitto Breeakpoint #2F mosquitto_user_data_set \r\n");
    rc = mosquitto_username_pw_set(mMosq, "mqtt_user", "P@$$#123");
    printf("MosquittoAppServer::InitMosquitto Breeakpoint #3 mosquitto_username_pw_set %s \r\n", rc==MOSQ_ERR_SUCCESS ? "succeeded" : "failed");
    
    printf("MosquittoAppServer::InitMosquitto Breeakpoint #4 about to connect to broker at address: %s \r\n", mBrokerIpAddr);
    
    rc = -1;
    rc = mosquitto_connect(mMosq, mBrokerIpAddr, 1883, 60);
    printf("MosquittoAppServer::InitMosquitto Breeakpoint #5 mosquitto_connect %s \r\n", rc==MOSQ_ERR_SUCCESS ? "succeeded" : "failed");
    if(rc != 0) {
        printf("MosquittoAppServer::InitMosquitto Client could not connect to MQTT-Broker!  Error Code: %d\n", rc);
      switch(rc) {
        case MOSQ_ERR_SUCCESS:{
            printf("MosquittoAppServer::InitMosquitto failed to connect.  Received Error MOSQ_ERR_SUCCESS \n");
            break;
        }
        case MOSQ_ERR_INVAL:{
            printf("MosquittoAppServer::InitMosquitto failed to connect.  Received Error MOSQ_ERR_INVAL \n");
            break;
        }
        case MOSQ_ERR_NOMEM:{
            printf("MosquittoAppServer::InitMosquitto failed to connect.  Received Error MOSQ_ERR_NOMEM \n");
            break;
        }
        case MOSQ_ERR_NO_CONN:{
            printf("MosquittoAppServer::InitMosquitto failed to connect.  Received Error MOSQ_ERR_NO_CONN \n");
            break;
        }
        case MOSQ_ERR_PROTOCOL:{
            printf("MosquittoAppServer::InitMosquitto failed to connect.  Received Error MOSQ_ERR_PROTOCOL \n");
            break;
        }
        case MOSQ_ERR_PAYLOAD_SIZE:{
            printf("MosquittoAppServer::InitMosquitto failed to connect.  Received Error MOSQ_ERR_PAYLOAD_SIZE \n");
            break;
        }
        case MOSQ_ERR_MALFORMED_UTF8:{
            printf("MosquittoAppServer::InitMosquitto failed to connect.  Received Error MOSQ_ERR_MALFORMED_UTF8 \n");
            break;
        }
        case MOSQ_ERR_QOS_NOT_SUPPORTED:{
            printf("MosquittoAppServer::InitMosquitto failed to connect.  Received Error MOSQ_ERR_QOS_NOT_SUPPORTED \n");
            break;
        }
        case MOSQ_ERR_OVERSIZE_PACKET:{
            printf("MosquittoAppServer::InitMosquitto failed to connect.  Received Error MOSQ_ERR_OVERSIZE_PACKET \n");
            break;
        }
      }
        mosquitto_destroy(mMosq);
        return;// -1;
    }
    printf("MosquittoAppServer::InitMosquitto Breeakpoint #6 - Now connected to the broker \r\n");  
    rc = mosquitto_publish(mMosq, NULL, "my-mqtt-topic", sizeof("Hello from a cleint!"), "Hello from a client!", 0, false);     
    rc = mosquitto_loop_start(mMosq);
    printf("MosquittoAppServer::InitMosquitto Breeakpoint #8  - mosquitto_loop_start  result: %s \r\n", rc==MOSQ_ERR_SUCCESS ? "succeeded" : "failed");
    
    printf("MosquittoAppServer::InitMosquitto Breeakpoint THE END \r\n");
    
    return rc == 0;
}
c++ mqtt mosquitto
1个回答
0
投票

根据场景,我的应用程序会初始化 mosquitto 两次,结果是两个循环不断尝试连接到代理。

© www.soinside.com 2019 - 2024. All rights reserved.