From 9ff9137e7a8b64e956c1c63e95a48f4049ad571e Mon Sep 17 00:00:00 2001 From: Akihiro YAMAZAKI Date: Thu, 14 Jan 2021 16:30:47 +0900 Subject: [PATCH] websocket: Add configurable ping interval Merges https://github.com/espressif/esp-idf/pull/6399 Signed-off-by: David Cermak --- .../esp_websocket_client/esp_websocket_client.c | 11 +++++++++-- .../include/esp_websocket_client.h | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/components/esp_websocket_client/esp_websocket_client.c b/components/esp_websocket_client/esp_websocket_client.c index 8a578c9315..167a9d4b07 100644 --- a/components/esp_websocket_client/esp_websocket_client.c +++ b/components/esp_websocket_client/esp_websocket_client.c @@ -37,7 +37,7 @@ static const char *TAG = "WEBSOCKET_CLIENT"; #define WEBSOCKET_TASK_PRIORITY (5) #define WEBSOCKET_TASK_STACK (4*1024) #define WEBSOCKET_NETWORK_TIMEOUT_MS (10*1000) -#define WEBSOCKET_PING_TIMEOUT_MS (10*1000) +#define WEBSOCKET_PING_INTERVAL_SEC (10) #define WEBSOCKET_EVENT_QUEUE_SIZE (1) #define WEBSOCKET_PINGPONG_TIMEOUT_SEC (120) #define WEBSOCKET_KEEP_ALIVE_IDLE (5) @@ -85,6 +85,7 @@ typedef struct { char *user_agent; char *headers; int pingpong_timeout_sec; + size_t ping_interval_sec; } websocket_config_storage_t; typedef enum { @@ -243,6 +244,12 @@ static esp_err_t esp_websocket_client_set_config(esp_websocket_client_handle_t c cfg->pingpong_timeout_sec = WEBSOCKET_PINGPONG_TIMEOUT_SEC; } + if (config->ping_interval_sec == 0) { + cfg->ping_interval_sec = WEBSOCKET_PING_INTERVAL_SEC; + } else { + cfg->ping_interval_sec = config->ping_interval_sec; + } + return ESP_OK; } @@ -612,7 +619,7 @@ static void esp_websocket_client_task(void *pv) case WEBSOCKET_STATE_CONNECTED: if ((CLOSE_FRAME_SENT_BIT & xEventGroupGetBits(client->status_bits)) == 0) { // only send and check for PING // if closing hasn't been initiated - if (_tick_get_ms() - client->ping_tick_ms > WEBSOCKET_PING_TIMEOUT_MS) { + if (_tick_get_ms() - client->ping_tick_ms > client->config->ping_interval_sec*1000) { client->ping_tick_ms = _tick_get_ms(); ESP_LOGD(TAG, "Sending PING..."); esp_transport_ws_send_raw(client->transport, WS_TRANSPORT_OPCODES_PING | WS_TRANSPORT_OPCODES_FIN, NULL, 0, client->config->network_timeout_ms); diff --git a/components/esp_websocket_client/include/esp_websocket_client.h b/components/esp_websocket_client/include/esp_websocket_client.h index c50184565a..13f817685e 100644 --- a/components/esp_websocket_client/include/esp_websocket_client.h +++ b/components/esp_websocket_client/include/esp_websocket_client.h @@ -99,6 +99,7 @@ typedef struct { int keep_alive_idle; /*!< Keep-alive idle time. Default is 5 (second) */ int keep_alive_interval; /*!< Keep-alive interval time. Default is 5 (second) */ int keep_alive_count; /*!< Keep-alive packet retry send count. Default is 3 counts */ + size_t ping_interval_sec; /*!< Websocket ping interval, defaults to 10 seconds if not set */ } esp_websocket_client_config_t; /**