c++ on_connect回调被连续调用

pod7payv  于 2023-03-20  发布在  其他
关注(0)|答案(1)|浏览(168)

我有一个简单的C++应用程序在本地运行,与Mosquitto代理程序在同一个机器上。我正在调试我的应用程序,我注意到on_connect回调函数一直在触发,代理程序一直显示以下两行。

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

这似乎是奇怪的行为,我想找出如何阻止它发生。
下面是我的代码,它设置了MQTT客户机并初始化到代理的连接。

  1. bool MosquittoAppServer::InitMosquitto() {
  2. int rc = mosquitto_lib_init();
  3. printf("MosquittoAppServer::InitMosquitto Breeakpoint #1 - mosquitto_lib_init %s \r\n", rc==MOSQ_ERR_SUCCESS ? "succeeded" : "failed");
  4. mMosq = mosquitto_new("publisher-test", true, NULL);
  5. printf("MosquittoAppServer::InitMosquitto Breeakpoint #2A %x %s \r\n",&mMosq, mMosq == NULL ? "failed to create mosquitto obj": "successfully created mosquitto obj");
  6. mosquitto_connect_callback_set(mMosq, on_connect_callback);
  7. printf("MosquittoAppServer::InitMosquitto Breeakpoint #2B mosquitto_connect_callback_set \r\n");
  8. mosquitto_publish_callback_set(mMosq, on_pub_callback);
  9. printf("MosquittoAppServer::InitMosquitto Breeakpoint #2C mosquitto_publish_callback_set \r\n");
  10. mosquitto_subscribe_callback_set(mMosq, on_sub_callback);
  11. printf("MosquittoAppServer::InitMosquitto Breeakpoint #2D mosquitto_subscribe_callback_set \r\n");
  12. mosquitto_message_callback_set(mMosq, on_msg_callback);
  13. printf("MosquittoAppServer::InitMosquitto Breeakpoint #2E mosquitto_message_callback_set \r\n");
  14. mosquitto_user_data_set(mMosq, this);
  15. printf("MosquittoAppServers::InitMosquitto Breeakpoint #2F mosquitto_user_data_set \r\n");
  16. rc = mosquitto_username_pw_set(mMosq, "mqtt_user", "P@$$#123");
  17. printf("MosquittoAppServer::InitMosquitto Breeakpoint #3 mosquitto_username_pw_set %s \r\n", rc==MOSQ_ERR_SUCCESS ? "succeeded" : "failed");
  18. printf("MosquittoAppServer::InitMosquitto Breeakpoint #4 about to connect to broker at address: %s \r\n", mBrokerIpAddr);
  19. rc = -1;
  20. rc = mosquitto_connect(mMosq, mBrokerIpAddr, 1883, 60);
  21. printf("MosquittoAppServer::InitMosquitto Breeakpoint #5 mosquitto_connect %s \r\n", rc==MOSQ_ERR_SUCCESS ? "succeeded" : "failed");
  22. if(rc != 0) {
  23. printf("MosquittoAppServer::InitMosquitto Client could not connect to MQTT-Broker! Error Code: %d\n", rc);
  24. switch(rc) {
  25. case MOSQ_ERR_SUCCESS:{
  26. printf("MosquittoAppServer::InitMosquitto failed to connect. Received Error MOSQ_ERR_SUCCESS \n");
  27. break;
  28. }
  29. case MOSQ_ERR_INVAL:{
  30. printf("MosquittoAppServer::InitMosquitto failed to connect. Received Error MOSQ_ERR_INVAL \n");
  31. break;
  32. }
  33. case MOSQ_ERR_NOMEM:{
  34. printf("MosquittoAppServer::InitMosquitto failed to connect. Received Error MOSQ_ERR_NOMEM \n");
  35. break;
  36. }
  37. case MOSQ_ERR_NO_CONN:{
  38. printf("MosquittoAppServer::InitMosquitto failed to connect. Received Error MOSQ_ERR_NO_CONN \n");
  39. break;
  40. }
  41. case MOSQ_ERR_PROTOCOL:{
  42. printf("MosquittoAppServer::InitMosquitto failed to connect. Received Error MOSQ_ERR_PROTOCOL \n");
  43. break;
  44. }
  45. case MOSQ_ERR_PAYLOAD_SIZE:{
  46. printf("MosquittoAppServer::InitMosquitto failed to connect. Received Error MOSQ_ERR_PAYLOAD_SIZE \n");
  47. break;
  48. }
  49. case MOSQ_ERR_MALFORMED_UTF8:{
  50. printf("MosquittoAppServer::InitMosquitto failed to connect. Received Error MOSQ_ERR_MALFORMED_UTF8 \n");
  51. break;
  52. }
  53. case MOSQ_ERR_QOS_NOT_SUPPORTED:{
  54. printf("MosquittoAppServer::InitMosquitto failed to connect. Received Error MOSQ_ERR_QOS_NOT_SUPPORTED \n");
  55. break;
  56. }
  57. case MOSQ_ERR_OVERSIZE_PACKET:{
  58. printf("MosquittoAppServer::InitMosquitto failed to connect. Received Error MOSQ_ERR_OVERSIZE_PACKET \n");
  59. break;
  60. }
  61. }
  62. mosquitto_destroy(mMosq);
  63. return;// -1;
  64. }
  65. printf("MosquittoAppServer::InitMosquitto Breeakpoint #6 - Now connected to the broker \r\n");
  66. rc = mosquitto_publish(mMosq, NULL, "my-mqtt-topic", sizeof("Hello from a cleint!"), "Hello from a client!", 0, false);
  67. rc = mosquitto_loop_start(mMosq);
  68. printf("MosquittoAppServer::InitMosquitto Breeakpoint #8 - mosquitto_loop_start result: %s \r\n", rc==MOSQ_ERR_SUCCESS ? "succeeded" : "failed");
  69. printf("MosquittoAppServer::InitMosquitto Breeakpoint THE END \r\n");
  70. return rc == 0;
  71. }
vxqlmq5t

vxqlmq5t1#

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

相关问题