Skip to content

Commit 341e480

Browse files
author
xutao
committed
websocket_client : fix some issues for websocket client
1. will post twice disconnect event when read error 2. will block `timeout` times when set disable_auto_connect 3. When `esp_websocket_client_stop` before `esp_websocket_client_send*`, if the `esp_websocket_client_send*` fails, the status will change to 'WEBSOCKET_STATE_WAIT_TIMEOUT', and the next `esp_websocket_client_start` will fail forever
1 parent ba717a2 commit 341e480

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

components/esp_websocket_client/esp_websocket_client.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ static const char *TAG = "WEBSOCKET_CLIENT";
4646
action; \
4747
}
4848

49+
#define ESP_WS_CLIENT_STATE_CHECK(TAG, a, action) if ((a->state) < WEBSOCKET_STATE_INIT) { \
50+
ESP_LOGE(TAG,"%s:%d (%s): %s", __FILE__, __LINE__, __FUNCTION__, "Websocket already stop"); \
51+
action; \
52+
}
53+
4954
const static int STOPPED_BIT = BIT0;
5055

5156
ESP_EVENT_DEFINE_BASE(WEBSOCKET_EVENTS);
@@ -134,11 +139,15 @@ static esp_err_t esp_websocket_client_dispatch_event(esp_websocket_client_handle
134139

135140
static esp_err_t esp_websocket_client_abort_connection(esp_websocket_client_handle_t client)
136141
{
142+
ESP_WS_CLIENT_STATE_CHECK(TAG, client, return ESP_FAIL);
137143
esp_transport_close(client->transport);
138-
client->wait_timeout_ms = WEBSOCKET_RECONNECT_TIMEOUT_MS;
139-
client->reconnect_tick_ms = _tick_get_ms();
144+
145+
if (client->config->auto_reconnect) {
146+
client->wait_timeout_ms = WEBSOCKET_RECONNECT_TIMEOUT_MS;
147+
client->reconnect_tick_ms = _tick_get_ms();
148+
ESP_LOGI(TAG, "Reconnect after %d ms", client->wait_timeout_ms);
149+
}
140150
client->state = WEBSOCKET_STATE_WAIT_TIMEOUT;
141-
ESP_LOGI(TAG, "Reconnect after %d ms", client->wait_timeout_ms);
142151
esp_websocket_client_dispatch_event(client, WEBSOCKET_EVENT_DISCONNECTED, NULL, 0);
143152
return ESP_OK;
144153
}
@@ -469,7 +478,6 @@ static esp_err_t esp_websocket_client_recv(esp_websocket_client_handle_t client)
469478
rlen = esp_transport_read(client->transport, client->rx_buffer, client->buffer_size, client->config->network_timeout_ms);
470479
if (rlen < 0) {
471480
ESP_LOGE(TAG, "Error read data");
472-
esp_websocket_client_abort_connection(client);
473481
return ESP_FAIL;
474482
}
475483
client->payload_len = esp_transport_ws_get_read_payload_len(client->transport);

0 commit comments

Comments
 (0)