Skip to content

Commit bced9ea

Browse files
committed
feat(websocket): allow using external tcp transport handle
1 parent 2b64e80 commit bced9ea

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

components/esp_websocket_client/esp_websocket_client.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ typedef struct {
9696
bool use_global_ca_store;
9797
bool skip_cert_common_name_check;
9898
esp_err_t (*crt_bundle_attach)(void *conf);
99+
esp_transport_handle_t ext_transport;
99100
} websocket_config_storage_t;
100101

101102
typedef enum {
@@ -474,6 +475,7 @@ static esp_err_t esp_websocket_client_create_transport(esp_websocket_client_hand
474475
ESP_WS_CLIENT_MEM_CHECK(TAG, client->transport_list, return ESP_ERR_NO_MEM);
475476
if (strcasecmp(client->config->scheme, WS_OVER_TCP_SCHEME) == 0) {
476477
esp_transport_handle_t tcp = esp_transport_tcp_init();
478+
477479
ESP_WS_CLIENT_MEM_CHECK(TAG, tcp, return ESP_ERR_NO_MEM);
478480

479481
esp_transport_set_default_port(tcp, WEBSOCKET_TCP_DEFAULT_PORT);
@@ -677,6 +679,7 @@ esp_websocket_client_handle_t esp_websocket_client_init(const esp_websocket_clie
677679
client->config->client_key_len = config->client_key_len;
678680
client->config->skip_cert_common_name_check = config->skip_cert_common_name_check;
679681
client->config->crt_bundle_attach = config->crt_bundle_attach;
682+
client->config->ext_transport = config->ext_transport;
680683

681684
if (config->uri) {
682685
if (esp_websocket_client_set_uri(client, config->uri) != ESP_OK) {
@@ -1100,9 +1103,13 @@ esp_err_t esp_websocket_client_start(esp_websocket_client_handle_t client)
11001103
ESP_LOGE(TAG, "The client has started");
11011104
return ESP_FAIL;
11021105
}
1103-
if (esp_websocket_client_create_transport(client) != ESP_OK) {
1104-
ESP_LOGE(TAG, "Failed to create websocket transport");
1105-
return ESP_FAIL;
1106+
1107+
client->transport = client->config->ext_transport;
1108+
if (!client->transport) {
1109+
if (esp_websocket_client_create_transport(client) != ESP_OK) {
1110+
ESP_LOGE(TAG, "Failed to create websocket transport");
1111+
return ESP_FAIL;
1112+
}
11061113
}
11071114

11081115
if (xTaskCreate(esp_websocket_client_task, client->config->task_name ? client->config->task_name : "websocket_task",

components/esp_websocket_client/include/esp_websocket_client.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ typedef struct {
127127
int network_timeout_ms; /*!< Abort network operation if it is not completed after this value, in milliseconds (defaults to 10s) */
128128
size_t ping_interval_sec; /*!< Websocket ping interval, defaults to 10 seconds if not set */
129129
struct ifreq *if_name; /*!< The name of interface for data to go through. Use the default interface without setting */
130+
esp_transport_handle_t ext_transport; /*!< External WebSocket tcp_transport handle to the client; or if null, the client will create its own transport handle. */
130131
} esp_websocket_client_config_t;
131132

132133
/**

0 commit comments

Comments
 (0)