Skip to content

Commit

Permalink
Merge pull request espressif#594 from erkia/erkia/ws-reconnect-timeout
Browse files Browse the repository at this point in the history
feat(esp_websocket_client): allow updating reconnect timeout for retry backoffs (IDFGH-13016)
  • Loading branch information
gabsuren authored Jul 3, 2024
2 parents 9152cfc + bd9f062 commit 25d8423
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
37 changes: 37 additions & 0 deletions components/esp_websocket_client/esp_websocket_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,43 @@ esp_err_t esp_websocket_client_set_ping_interval_sec(esp_websocket_client_handle
return ESP_OK;
}

int esp_websocket_client_get_reconnect_timeout(esp_websocket_client_handle_t client)
{
if (client == NULL) {
ESP_LOGW(TAG, "Client was not initialized");
return -1;
}

if (!client->config->auto_reconnect) {
ESP_LOGW(TAG, "Automatic reconnect is disabled");
return -1;
}

return client->wait_timeout_ms;
}

esp_err_t esp_websocket_client_set_reconnect_timeout(esp_websocket_client_handle_t client, int reconnect_timeout_ms)
{
if (client == NULL) {
ESP_LOGW(TAG, "Client was not initialized");
return ESP_ERR_INVALID_ARG;
}

if (reconnect_timeout_ms <= 0) {
ESP_LOGW(TAG, "Invalid reconnect timeout");
return ESP_ERR_INVALID_ARG;
}

if (!client->config->auto_reconnect) {
ESP_LOGW(TAG, "Automatic reconnect is disabled");
return ESP_ERR_INVALID_STATE;
}

client->wait_timeout_ms = reconnect_timeout_ms;

return ESP_OK;
}

esp_err_t esp_websocket_register_events(esp_websocket_client_handle_t client,
esp_websocket_event_id_t event,
esp_event_handler_t event_handler,
Expand Down
23 changes: 23 additions & 0 deletions components/esp_websocket_client/include/esp_websocket_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,29 @@ size_t esp_websocket_client_get_ping_interval_sec(esp_websocket_client_handle_t
*/
esp_err_t esp_websocket_client_set_ping_interval_sec(esp_websocket_client_handle_t client, size_t ping_interval_sec);

/**
* @brief Get the next reconnect timeout for client. Returns -1 when client is not initialized or automatic reconnect is disabled.
*
* @param[in] client The client
*
* @return Reconnect timeout in msec
*/
int esp_websocket_client_get_reconnect_timeout(esp_websocket_client_handle_t client);

/**
* @brief Set next reconnect timeout for client.
*
* Notes:
* - Changing this value when reconnection delay is already active does not immediately affect the active delay and may have unexpected result.
* - Good place to change this value is when handling WEBSOCKET_EVENT_DISCONNECTED or WEBSOCKET_EVENT_ERROR events.
*
* @param[in] client The client
* @param[in] reconnect_timeout_ms The new timeout
*
* @return esp_err_t
*/
esp_err_t esp_websocket_client_set_reconnect_timeout(esp_websocket_client_handle_t client, int reconnect_timeout_ms);

/**
* @brief Register the Websocket Events
*
Expand Down

0 comments on commit 25d8423

Please sign in to comment.