我有一个简单的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;
}
1条答案
按热度按时间vxqlmq5t1#
根据不同的场景,我的应用程序会初始化mosquito两次,结果是两个循环不断地尝试连接到代理。