Skip to content

Commit 0625628

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

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

components/esp_websocket_client/esp_websocket_client.c

Lines changed: 13 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;
@@ -1312,6 +1320,10 @@ esp_err_t esp_websocket_client_set_reconnect_timeout(esp_websocket_client_handle
13121320

13131321
client->wait_timeout_ms = reconnect_timeout_ms;
13141322

1323+
if (client->task_handle) {
1324+
xTaskNotifyGive(client->task_handle);
1325+
}
1326+
13151327
return ESP_OK;
13161328
}
13171329

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)