Skip to content

Commit 0ccc3a6

Browse files
committed
feat(websocket): interruptable wait timeout
1 parent 25d8423 commit 0ccc3a6

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

components/esp_websocket_client/esp_websocket_client.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1063,8 +1063,13 @@ static void esp_websocket_client_task(void *pv)
10631063
esp_websocket_client_abort_connection(client, WEBSOCKET_ERROR_TYPE_TCP_TRANSPORT);
10641064
}
10651065
} else if (WEBSOCKET_STATE_WAIT_TIMEOUT == client->state) {
1066+
// clear any pending notifications
1067+
ulTaskNotifyTake(pdTRUE, 0);
10661068
// waiting for reconnecting...
1067-
vTaskDelay(client->wait_timeout_ms / 2 / portTICK_PERIOD_MS);
1069+
int delay = client->wait_timeout_ms - (_tick_get_ms() - client->reconnect_tick_ms);
1070+
if (client->run && delay >= 0) {
1071+
ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(delay) + 1);
1072+
}
10681073
} else if (WEBSOCKET_STATE_CLOSING == client->state &&
10691074
(CLOSE_FRAME_SENT_BIT & xEventGroupGetBits(client->status_bits))) {
10701075
ESP_LOGD(TAG, " Waiting for TCP connection to be closed by the server");
@@ -1133,6 +1138,9 @@ esp_err_t esp_websocket_client_stop(esp_websocket_client_handle_t client)
11331138

11341139

11351140
client->run = false;
1141+
if (client->task_handle) {
1142+
xTaskNotifyGive(client->task_handle);
1143+
}
11361144
xEventGroupWaitBits(client->status_bits, STOPPED_BIT, false, true, portMAX_DELAY);
11371145
client->state = WEBSOCKET_STATE_UNKNOW;
11381146
return ESP_OK;
@@ -1188,6 +1196,9 @@ static esp_err_t esp_websocket_client_close_with_optional_body(esp_websocket_cli
11881196

11891197
// If could not close gracefully within timeout, stop the client and disconnect
11901198
client->run = false;
1199+
if (client->task_handle) {
1200+
xTaskNotifyGive(client->task_handle);
1201+
}
11911202
xEventGroupWaitBits(client->status_bits, STOPPED_BIT, false, true, portMAX_DELAY);
11921203
client->state = WEBSOCKET_STATE_UNKNOW;
11931204
return ESP_OK;
@@ -1312,6 +1323,10 @@ esp_err_t esp_websocket_client_set_reconnect_timeout(esp_websocket_client_handle
13121323

13131324
client->wait_timeout_ms = reconnect_timeout_ms;
13141325

1326+
if (client->task_handle) {
1327+
xTaskNotifyGive(client->task_handle);
1328+
}
1329+
13151330
return ESP_OK;
13161331
}
13171332

components/esp_websocket_client/include/esp_websocket_client.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ size_t esp_websocket_client_get_ping_interval_sec(esp_websocket_client_handle_t
417417
esp_err_t esp_websocket_client_set_ping_interval_sec(esp_websocket_client_handle_t client, size_t ping_interval_sec);
418418

419419
/**
420-
* @brief Get the next reconnect timeout for client. Returns -1 when client is not initialized or automatic reconnect is disabled.
420+
* @brief Get the reconnect timeout for client. Returns -1 when client is not initialized or automatic reconnect is disabled.
421421
*
422422
* @param[in] client The client
423423
*
@@ -426,11 +426,10 @@ esp_err_t esp_websocket_client_set_ping_interval_sec(esp_websocket_client_handle
426426
int esp_websocket_client_get_reconnect_timeout(esp_websocket_client_handle_t client);
427427

428428
/**
429-
* @brief Set next reconnect timeout for client.
429+
* @brief Set new reconnect timeout for client.
430430
*
431431
* Notes:
432-
* - Changing this value when reconnection delay is already active does not immediately affect the active delay and may have unexpected result.
433-
* - Good place to change this value is when handling WEBSOCKET_EVENT_DISCONNECTED or WEBSOCKET_EVENT_ERROR events.
432+
* - This also updates already active reconnection delay, if any.
434433
*
435434
* @param[in] client The client
436435
* @param[in] reconnect_timeout_ms The new timeout

0 commit comments

Comments
 (0)