websocket: Added configs reconnect_timeout_ms and network_timeout_ms

Closes https://github.com/espressif/esp-idf/issues/8263
This commit is contained in:
gabsuren 2022-01-31 16:43:07 +04:00 committed by bot
parent 8cfff5283b
commit 6c26d65203
2 changed files with 16 additions and 3 deletions

View File

@ -153,7 +153,6 @@ static esp_err_t esp_websocket_client_abort_connection(esp_websocket_client_hand
esp_transport_close(client->transport); esp_transport_close(client->transport);
if (client->config->auto_reconnect) { if (client->config->auto_reconnect) {
client->wait_timeout_ms = WEBSOCKET_RECONNECT_TIMEOUT_MS;
client->reconnect_tick_ms = _tick_get_ms(); client->reconnect_tick_ms = _tick_get_ms();
ESP_LOGI(TAG, "Reconnect after %d ms", client->wait_timeout_ms); ESP_LOGI(TAG, "Reconnect after %d ms", client->wait_timeout_ms);
} }
@ -222,7 +221,7 @@ static esp_err_t esp_websocket_client_set_config(esp_websocket_client_handle_t c
ESP_WS_CLIENT_MEM_CHECK(TAG, cfg->headers, return ESP_ERR_NO_MEM); ESP_WS_CLIENT_MEM_CHECK(TAG, cfg->headers, return ESP_ERR_NO_MEM);
} }
cfg->network_timeout_ms = WEBSOCKET_NETWORK_TIMEOUT_MS;
cfg->user_context = config->user_context; cfg->user_context = config->user_context;
cfg->auto_reconnect = true; cfg->auto_reconnect = true;
if (config->disable_auto_reconnect) { if (config->disable_auto_reconnect) {
@ -237,6 +236,13 @@ static esp_err_t esp_websocket_client_set_config(esp_websocket_client_handle_t c
cfg->pingpong_timeout_sec = WEBSOCKET_PINGPONG_TIMEOUT_SEC; cfg->pingpong_timeout_sec = WEBSOCKET_PINGPONG_TIMEOUT_SEC;
} }
if (config->network_timeout_ms <= 0) {
cfg->network_timeout_ms = WEBSOCKET_NETWORK_TIMEOUT_MS;
ESP_LOGW(TAG, "`network_timeout_ms` is not set, or it is less than or equal to zero, using default time out %d (milliseconds)", WEBSOCKET_NETWORK_TIMEOUT_MS);
} else {
cfg->network_timeout_ms = config->network_timeout_ms;
}
if (config->ping_interval_sec == 0) { if (config->ping_interval_sec == 0) {
cfg->ping_interval_sec = WEBSOCKET_PING_INTERVAL_SEC; cfg->ping_interval_sec = WEBSOCKET_PING_INTERVAL_SEC;
} else { } else {
@ -373,7 +379,12 @@ esp_websocket_client_handle_t esp_websocket_client_init(const esp_websocket_clie
if (config->skip_cert_common_name_check) { if (config->skip_cert_common_name_check) {
esp_transport_ssl_skip_common_name_check(ssl); esp_transport_ssl_skip_common_name_check(ssl);
} }
if (config->reconnect_timeout_ms <= 0) {
client->wait_timeout_ms = WEBSOCKET_RECONNECT_TIMEOUT_MS;
ESP_LOGW(TAG, "`reconnect_timeout_ms` is not set, or it is less than or equal to zero, using default time out %d (milliseconds)", WEBSOCKET_RECONNECT_TIMEOUT_MS);
} else {
client->wait_timeout_ms = config->reconnect_timeout_ms;
}
esp_transport_handle_t wss = esp_transport_ws_init(ssl); esp_transport_handle_t wss = esp_transport_ws_init(ssl);
ESP_WS_CLIENT_MEM_CHECK(TAG, wss, goto _websocket_init_fail); ESP_WS_CLIENT_MEM_CHECK(TAG, wss, goto _websocket_init_fail);

View File

@ -92,6 +92,8 @@ typedef struct {
int keep_alive_idle; /*!< Keep-alive idle time. Default is 5 (second) */ int keep_alive_idle; /*!< Keep-alive idle time. Default is 5 (second) */
int keep_alive_interval; /*!< Keep-alive interval time. Default is 5 (second) */ int keep_alive_interval; /*!< Keep-alive interval time. Default is 5 (second) */
int keep_alive_count; /*!< Keep-alive packet retry send count. Default is 3 counts */ int keep_alive_count; /*!< Keep-alive packet retry send count. Default is 3 counts */
int reconnect_timeout_ms; /*!< Reconnect after this value in miliseconds if disable_auto_reconnect is not enabled (defaults to 10s) */
int network_timeout_ms; /*!< Abort network operation if it is not completed after this value, in milliseconds (defaults to 10s) */
size_t ping_interval_sec; /*!< Websocket ping interval, defaults to 10 seconds if not set */ size_t ping_interval_sec; /*!< Websocket ping interval, defaults to 10 seconds if not set */
struct ifreq *if_name; /*!< The name of interface for data to go through. Use the default interface without setting */ struct ifreq *if_name; /*!< The name of interface for data to go through. Use the default interface without setting */
} esp_websocket_client_config_t; } esp_websocket_client_config_t;