Skip to content

Commit bd9f062

Browse files
committed
feat(websocket): allow updating reconnect timeout for retry backoffs
1 parent 9152cfc commit bd9f062

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

components/esp_websocket_client/esp_websocket_client.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,6 +1278,43 @@ esp_err_t esp_websocket_client_set_ping_interval_sec(esp_websocket_client_handle
12781278
return ESP_OK;
12791279
}
12801280

1281+
int esp_websocket_client_get_reconnect_timeout(esp_websocket_client_handle_t client)
1282+
{
1283+
if (client == NULL) {
1284+
ESP_LOGW(TAG, "Client was not initialized");
1285+
return -1;
1286+
}
1287+
1288+
if (!client->config->auto_reconnect) {
1289+
ESP_LOGW(TAG, "Automatic reconnect is disabled");
1290+
return -1;
1291+
}
1292+
1293+
return client->wait_timeout_ms;
1294+
}
1295+
1296+
esp_err_t esp_websocket_client_set_reconnect_timeout(esp_websocket_client_handle_t client, int reconnect_timeout_ms)
1297+
{
1298+
if (client == NULL) {
1299+
ESP_LOGW(TAG, "Client was not initialized");
1300+
return ESP_ERR_INVALID_ARG;
1301+
}
1302+
1303+
if (reconnect_timeout_ms <= 0) {
1304+
ESP_LOGW(TAG, "Invalid reconnect timeout");
1305+
return ESP_ERR_INVALID_ARG;
1306+
}
1307+
1308+
if (!client->config->auto_reconnect) {
1309+
ESP_LOGW(TAG, "Automatic reconnect is disabled");
1310+
return ESP_ERR_INVALID_STATE;
1311+
}
1312+
1313+
client->wait_timeout_ms = reconnect_timeout_ms;
1314+
1315+
return ESP_OK;
1316+
}
1317+
12811318
esp_err_t esp_websocket_register_events(esp_websocket_client_handle_t client,
12821319
esp_websocket_event_id_t event,
12831320
esp_event_handler_t event_handler,

components/esp_websocket_client/include/esp_websocket_client.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,29 @@ size_t esp_websocket_client_get_ping_interval_sec(esp_websocket_client_handle_t
416416
*/
417417
esp_err_t esp_websocket_client_set_ping_interval_sec(esp_websocket_client_handle_t client, size_t ping_interval_sec);
418418

419+
/**
420+
* @brief Get the next reconnect timeout for client. Returns -1 when client is not initialized or automatic reconnect is disabled.
421+
*
422+
* @param[in] client The client
423+
*
424+
* @return Reconnect timeout in msec
425+
*/
426+
int esp_websocket_client_get_reconnect_timeout(esp_websocket_client_handle_t client);
427+
428+
/**
429+
* @brief Set next reconnect timeout for client.
430+
*
431+
* 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.
434+
*
435+
* @param[in] client The client
436+
* @param[in] reconnect_timeout_ms The new timeout
437+
*
438+
* @return esp_err_t
439+
*/
440+
esp_err_t esp_websocket_client_set_reconnect_timeout(esp_websocket_client_handle_t client, int reconnect_timeout_ms);
441+
419442
/**
420443
* @brief Register the Websocket Events
421444
*

0 commit comments

Comments
 (0)