]> granicus.if.org Git - esp-idf/commitdiff
ws_client: added subprotocol configuration option to websocket client
authorDavid Cermak <cermak@espressif.com>
Thu, 22 Aug 2019 20:00:41 +0000 (22:00 +0200)
committerDavid Cermak <cermak@espressif.com>
Thu, 3 Oct 2019 05:33:32 +0000 (07:33 +0200)
closes https://github.com/espressif/esp-idf/issues/3893

components/esp_websocket_client/esp_websocket_client.c
components/esp_websocket_client/include/esp_websocket_client.h

index 6bd1cf2aefdba9281ad2816e9f58692d44e0dc45..dc73334190bd02e5610a6919541634978c537da1 100644 (file)
@@ -63,6 +63,7 @@ typedef struct {
     bool                        auto_reconnect;
     void                        *user_context;
     int                         network_timeout_ms;
+    char                        *subprotocol;
 } websocket_config_storage_t;
 
 typedef enum {
@@ -172,6 +173,11 @@ static esp_err_t esp_websocket_client_set_config(esp_websocket_client_handle_t c
         cfg->path = strdup(config->path);
         ESP_WS_CLIENT_MEM_CHECK(TAG, cfg->path, return ESP_ERR_NO_MEM);
     }
+    if (config->subprotocol) {
+        free(cfg->subprotocol);
+        cfg->subprotocol = strdup(config->subprotocol);
+        ESP_WS_CLIENT_MEM_CHECK(TAG, cfg->subprotocol, return ESP_ERR_NO_MEM);
+    }
 
     cfg->network_timeout_ms = WEBSOCKET_NETWORK_TIMEOUT_MS;
     cfg->user_context = config->user_context;
@@ -199,21 +205,20 @@ static esp_err_t esp_websocket_client_destroy_config(esp_websocket_client_handle
     free(cfg->scheme);
     free(cfg->username);
     free(cfg->password);
+    free(cfg->subprotocol);
     memset(cfg, 0, sizeof(websocket_config_storage_t));
     free(client->config);
     client->config = NULL;
     return ESP_OK;
 }
 
-static void set_websocket_client_path(esp_websocket_client_handle_t client)
+static void set_websocket_transport_optional_settings(esp_websocket_client_handle_t client, esp_transport_handle_t trans)
 {
-    esp_transport_handle_t trans = esp_transport_list_get_transport(client->transport_list, "ws");
-    if (trans) {
+    if (trans && client->config->path) {
         esp_transport_ws_set_path(trans, client->config->path);
     }
-    trans = esp_transport_list_get_transport(client->transport_list, "wss");
-    if (trans) {
-        esp_transport_ws_set_path(trans, client->config->path);
+    if (trans && client->config->subprotocol) {
+        esp_transport_ws_set_subprotocol(trans, client->config->subprotocol);
     }
 }
 
@@ -296,9 +301,8 @@ esp_websocket_client_handle_t esp_websocket_client_init(const esp_websocket_clie
         ESP_WS_CLIENT_MEM_CHECK(TAG, client->config->scheme, goto _websocket_init_fail);
     }
 
-    if (client->config->path) {
-        set_websocket_client_path(client);
-    }
+    set_websocket_transport_optional_settings(client, esp_transport_list_get_transport(client->transport_list, "ws"));
+    set_websocket_transport_optional_settings(client, esp_transport_list_get_transport(client->transport_list, "wss"));
 
     client->keepalive_tick_ms = _tick_get_ms();
     client->reconnect_tick_ms = _tick_get_ms();
@@ -382,7 +386,6 @@ esp_err_t esp_websocket_client_set_uri(esp_websocket_client_handle_t client, con
         free(client->config->path);
         asprintf(&client->config->path, "%.*s", puri.field_data[UF_PATH].len, uri + puri.field_data[UF_PATH].off);
         ESP_WS_CLIENT_MEM_CHECK(TAG, client->config->path, return ESP_ERR_NO_MEM);
-        set_websocket_client_path(client);
     }
     if (puri.field_data[UF_PORT].off) {
         client->config->port = strtol((const char*)(uri + puri.field_data[UF_PORT].off), NULL, 10);
index a8bcc5e2f48a7692c41461267e62cc601cff8b4f..4f1e0a3fc0cdd18da4fe517ba32b0808c67d0a62 100644 (file)
@@ -91,6 +91,7 @@ typedef struct {
     int                         buffer_size;                /*!< Websocket buffer size */
     const char                  *cert_pem;                  /*!< SSL Certification, PEM format as string, if the client requires to verify server */
     esp_websocket_transport_t   transport;                  /*!< Websocket transport type, see `esp_websocket_transport_t */
+    char                        *subprotocol;               /*!< Websocket subprotocol */
 } esp_websocket_client_config_t;
 
 /**