From 0d8f2a6d53b306803932e9c98522c81898aa9b6b Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Date: Mon, 12 Aug 2024 16:19:46 +0200 Subject: [PATCH] fix(websocket): Move client to different state when disconnecting If Websocket client is set to not reconnect, we move to Unknown state to clean up the task after dispatching disconnected event. --- .../esp_websocket_client/esp_websocket_client.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/components/esp_websocket_client/esp_websocket_client.c b/components/esp_websocket_client/esp_websocket_client.c index 4095960383..713e244b7f 100644 --- a/components/esp_websocket_client/esp_websocket_client.c +++ b/components/esp_websocket_client/esp_websocket_client.c @@ -227,13 +227,15 @@ static esp_err_t esp_websocket_client_abort_connection(esp_websocket_client_hand ESP_WS_CLIENT_STATE_CHECK(TAG, client, return ESP_FAIL); esp_transport_close(client->transport); - if (client->config->auto_reconnect) { + if (!client->config->auto_reconnect) { + client->run = false; + client->state = WEBSOCKET_STATE_UNKNOW; + } else { client->reconnect_tick_ms = _tick_get_ms(); ESP_LOGI(TAG, "Reconnect after %d ms", client->wait_timeout_ms); + client->error_handle.error_type = error_type; + client->state = WEBSOCKET_STATE_WAIT_TIMEOUT; } - - client->error_handle.error_type = error_type; - client->state = WEBSOCKET_STATE_WAIT_TIMEOUT; esp_websocket_client_dispatch_event(client, WEBSOCKET_EVENT_DISCONNECTED, NULL, 0); return ESP_OK; } @@ -1048,10 +1050,6 @@ static void esp_websocket_client_task(void *pv) break; case WEBSOCKET_STATE_WAIT_TIMEOUT: - if (!client->config->auto_reconnect) { - client->run = false; - break; - } if (_tick_get_ms() - client->reconnect_tick_ms > client->wait_timeout_ms) { client->state = WEBSOCKET_STATE_INIT; client->reconnect_tick_ms = _tick_get_ms();