Skip to content

Commit

Permalink
fix(websocket): Move client to different state when disconnecting
Browse files Browse the repository at this point in the history
If Websocket client is set to not reconnect, we move to Unknown state to
clean up the task after dispatching disconnected event.
  • Loading branch information
euripedesrocha committed Aug 19, 2024
1 parent 7e5ac87 commit 0d8f2a6
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions components/esp_websocket_client/esp_websocket_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 0d8f2a6

Please sign in to comment.