基于C++在windows下使用mosquitto实现MQTT订阅客户端
1、MOSQUITTO订阅客户端搭建
1 #include <iostream> 2 #include <iostream> 3 #include <thread> 4 #include <mosquitto.h> 5 6 //mqtt 7 #define MQTT_USER_NAME "admin" 8 #define MQTT_PASS_WORD "123456" 9 #define MQTT_SERVER_IP_ADDRESS "10.57.6.250" 10 #define MQTT_SERVER_PORT 1883 11 #define PUBLISH_JSON_DATA_ORG "4321" 12 static struct mosquitto* mosq; 13 static bool mqtt_init_sign; 14 //mqtt 15 16 void on_message(struct mosquitto* mosq, void* userdata, const struct mosquitto_message* msg) { 17 std::cout << "\nrev msg:" << std::endl; 18 std::cout << "the topic of the msg is: " << msg->topic << std::endl; 19 20 if (msg->payloadlen > 0) { 21 std::string message(static_cast<char*>(msg->payload), msg->payloadlen); 22 std::cout << "the msg content is: " << message.c_str() << std::endl; 23 } 24 else { 25 std::cout << "the msg content is:null" << std::endl; 26 } 27 28 std::cout << "QoS: " << msg->qos << std::endl; 29 std::cout << "the ID of the msg is: " << msg->mid << std::endl; 30 std::cout << "---------------------" << std::endl; 31 } 32 33 void on_connect(struct mosquitto *mosq, void *obj, int rc) { 34 if (rc == 0) { 35 printf("authentication success\n"); 36 } 37 else { 38 printf("connect failed: %s\n", mosquitto_strerror(rc)); 39 } 40 } 41 42 void on_disconnect(struct mosquitto *mosq, void *obj, int rc) { 43 printf("disconnect: %s\n", mosquitto_strerror(rc)); 44 45 if (rc == MOSQ_ERR_AUTH) { 46 printf("error:authentication failure(username/password is wrong)\n"); 47 } 48 else if (rc == MOSQ_ERR_CONN_REFUSED) { 49 printf("error:access refused\n"); 50 } 51 } 52 53 int init_mqtt(const std::string username, const std::string passwd, const std::string hostname, const int port) 54 { 55 mosquitto_lib_init(); 56 57 mosq = mosquitto_new(nullptr, true, nullptr);//第一个形参制定为一个值后,派生类会出现发布数据失败的情况,因为不同的派生类共用一个client_id 58 if (!mosq) { 59 std::cerr << "Failed to create mosquitto instance!" << std::endl; 60 return 0; 61 } 62 63 mosquitto_connect_callback_set(mosq, on_connect); 64 mosquitto_disconnect_callback_set(mosq, on_disconnect); 65 mosquitto_message_callback_set(mosq, on_message); 66 67 const char* usrname = username.c_str(); 68 const char* password = passwd.c_str(); 69 70 int rc = mosquitto_username_pw_set(mosq, usrname, password); 71 if (rc != MOSQ_ERR_SUCCESS) { 72 std::cerr << "Failed to set username/password: " << mosquitto_strerror(rc) << std::endl; 73 mosquitto_destroy(mosq); 74 mosquitto_lib_cleanup(); 75 return 0; 76 } 77 78 rc = mosquitto_connect(mosq, hostname.c_str(), port, 60); 79 if (rc != MOSQ_ERR_SUCCESS) { 80 std::cerr << "Connection failed: " << mosquitto_strerror(rc) << std::endl; 81 82 std::cout << "Trying without authentication..." << std::endl; 83 mosquitto_username_pw_set(mosq, nullptr, nullptr); 84 85 rc = mosquitto_connect(mosq, hostname.c_str(), port, 60); 86 if (rc != MOSQ_ERR_SUCCESS) { 87 std::cerr << "Still failed: " << mosquitto_strerror(rc) << std::endl; 88 mosquitto_destroy(mosq); 89 mosquitto_lib_cleanup(); 90 return 0; 91 } 92 } 93 94 mosquitto_loop_start(mosq); 95 mqtt_init_sign = true; 96 return 1; 97 } 98 99 void subscribe_msg_from_server() 100 { 101 const char* topic = "TestMqttTopic"; 102 int qos = 1; // 服务质量等级 0,1,2 103 104 int result = mosquitto_subscribe(mosq, NULL, topic, qos); 105 if (result != MOSQ_ERR_SUCCESS) { 106 std::cerr << "订阅失败: " << mosquitto_strerror(result) << std::endl; 107 } 108 else { 109 std::cout << "已订阅主题: " << topic << std::endl; 110 } 111 } 112 113 int main() 114 { 115 init_mqtt(MQTT_USER_NAME, MQTT_PASS_WORD, MQTT_SERVER_IP_ADDRESS, MQTT_SERVER_PORT); 116 117 while (1) 118 { 119 std::this_thread::sleep_for(std::chrono::milliseconds(5000)); 120 std::cout << "start subscribe!" << std::endl; 121 subscribe_msg_from_server(); 122 } 123 }
2、效果展示

记性太差,需要这么记下来

浙公网安备 33010602011771号