Browse Source

Add mqtt_client_set_host function (#601)

龚辟愚 1 year ago
parent
commit
5ac47b326d
2 changed files with 11 additions and 0 deletions
  1. 6 0
      mqtt/mqtt_client.c
  2. 5 0
      mqtt/mqtt_client.h

+ 6 - 0
mqtt/mqtt_client.c

@@ -485,6 +485,12 @@ void mqtt_client_set_connect_timeout(mqtt_client_t* cli, int ms) {
     cli->connect_timeout = ms;
 }
 
+void mqtt_client_set_host(mqtt_client_t* cli, const char* host, int port, int ssl) {
+    hv_strncpy(cli->host, host, sizeof(cli->host));
+    cli->port = port;
+    cli->ssl = ssl;
+}
+
 int mqtt_client_connect(mqtt_client_t* cli, const char* host, int port, int ssl) {
     if (!cli) return -1;
     hv_strncpy(cli->host, host, sizeof(cli->host));

+ 5 - 0
mqtt/mqtt_client.h

@@ -102,6 +102,7 @@ HV_EXPORT int mqtt_client_reconnect(mqtt_client_t* cli);
 // on_connect -> mqtt_client_login ->
 // on_connack
 HV_EXPORT void mqtt_client_set_connect_timeout(mqtt_client_t* cli, int ms);
+HV_EXPORT void mqtt_client_set_host(mqtt_client_t* cli, const char* host, int port, int ssl);
 HV_EXPORT int  mqtt_client_connect(mqtt_client_t* cli,
         const char* host,
         int port DEFAULT(DEFAULT_MQTT_PORT),
@@ -203,6 +204,10 @@ public:
         mqtt_client_set_connect_timeout(client, ms);
     }
 
+    void setHost(const char* host, int port = DEFAULT_MQTT_PORT, int ssl = 0) {
+        mqtt_client_set_host(client, host, port, ssl);
+    }
+
     int connect(const char* host, int port = DEFAULT_MQTT_PORT, int ssl = 0) {
         return mqtt_client_connect(client, host, port, ssl);
     }