mirror of
https://github.com/espressif/esp-protocols.git
synced 2025-07-16 03:52:13 +02:00
Add options for esp_http_client and esp_websocket_client to support keepalive
* Original commit: espressif/esp-idf@b53e46a68e
This commit is contained in:
committed by
gabsuren
parent
d1dd6ece38
commit
8a6c320a29
@ -40,6 +40,9 @@ static const char *TAG = "WEBSOCKET_CLIENT";
|
|||||||
#define WEBSOCKET_PING_TIMEOUT_MS (10*1000)
|
#define WEBSOCKET_PING_TIMEOUT_MS (10*1000)
|
||||||
#define WEBSOCKET_EVENT_QUEUE_SIZE (1)
|
#define WEBSOCKET_EVENT_QUEUE_SIZE (1)
|
||||||
#define WEBSOCKET_PINGPONG_TIMEOUT_SEC (120)
|
#define WEBSOCKET_PINGPONG_TIMEOUT_SEC (120)
|
||||||
|
#define WEBSOCKET_KEEP_ALIVE_IDLE (5)
|
||||||
|
#define WEBSOCKET_KEEP_ALIVE_INTERVAL (5)
|
||||||
|
#define WEBSOCKET_KEEP_ALIVE_COUNT (3)
|
||||||
|
|
||||||
#define ESP_WS_CLIENT_MEM_CHECK(TAG, a, action) if (!(a)) { \
|
#define ESP_WS_CLIENT_MEM_CHECK(TAG, a, action) if (!(a)) { \
|
||||||
ESP_LOGE(TAG,"%s(%d): %s", __FUNCTION__, __LINE__, "Memory exhausted"); \
|
ESP_LOGE(TAG,"%s(%d): %s", __FUNCTION__, __LINE__, "Memory exhausted"); \
|
||||||
@ -108,6 +111,7 @@ struct esp_websocket_client {
|
|||||||
ws_transport_opcodes_t last_opcode;
|
ws_transport_opcodes_t last_opcode;
|
||||||
int payload_len;
|
int payload_len;
|
||||||
int payload_offset;
|
int payload_offset;
|
||||||
|
esp_transport_keep_alive_t keep_alive_cfg;
|
||||||
};
|
};
|
||||||
|
|
||||||
static uint64_t _tick_get_ms(void)
|
static uint64_t _tick_get_ms(void)
|
||||||
@ -290,6 +294,13 @@ esp_websocket_client_handle_t esp_websocket_client_init(const esp_websocket_clie
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (config->keep_alive_enable == true) {
|
||||||
|
client->keep_alive_cfg.keep_alive_enable = true;
|
||||||
|
client->keep_alive_cfg.keep_alive_idle = (config->keep_alive_idle == 0) ? WEBSOCKET_KEEP_ALIVE_IDLE : config->keep_alive_idle;
|
||||||
|
client->keep_alive_cfg.keep_alive_interval = (config->keep_alive_interval == 0) ? WEBSOCKET_KEEP_ALIVE_INTERVAL : config->keep_alive_interval;
|
||||||
|
client->keep_alive_cfg.keep_alive_count = (config->keep_alive_count == 0) ? WEBSOCKET_KEEP_ALIVE_COUNT : config->keep_alive_count;
|
||||||
|
}
|
||||||
|
|
||||||
client->lock = xSemaphoreCreateRecursiveMutex();
|
client->lock = xSemaphoreCreateRecursiveMutex();
|
||||||
ESP_WS_CLIENT_MEM_CHECK(TAG, client->lock, goto _websocket_init_fail);
|
ESP_WS_CLIENT_MEM_CHECK(TAG, client->lock, goto _websocket_init_fail);
|
||||||
|
|
||||||
@ -303,6 +314,7 @@ esp_websocket_client_handle_t esp_websocket_client_init(const esp_websocket_clie
|
|||||||
ESP_WS_CLIENT_MEM_CHECK(TAG, tcp, goto _websocket_init_fail);
|
ESP_WS_CLIENT_MEM_CHECK(TAG, tcp, goto _websocket_init_fail);
|
||||||
|
|
||||||
esp_transport_set_default_port(tcp, WEBSOCKET_TCP_DEFAULT_PORT);
|
esp_transport_set_default_port(tcp, WEBSOCKET_TCP_DEFAULT_PORT);
|
||||||
|
esp_transport_tcp_set_keep_alive(tcp, &client->keep_alive_cfg);
|
||||||
esp_transport_list_add(client->transport_list, tcp, "_tcp"); // need to save to transport list, for cleanup
|
esp_transport_list_add(client->transport_list, tcp, "_tcp"); // need to save to transport list, for cleanup
|
||||||
|
|
||||||
|
|
||||||
@ -346,6 +358,7 @@ esp_websocket_client_handle_t esp_websocket_client_init(const esp_websocket_clie
|
|||||||
if (config->skip_cert_common_name_check) {
|
if (config->skip_cert_common_name_check) {
|
||||||
esp_transport_ssl_skip_common_name_check(ssl);
|
esp_transport_ssl_skip_common_name_check(ssl);
|
||||||
}
|
}
|
||||||
|
esp_transport_ssl_set_keep_alive(ssl, &client->keep_alive_cfg);
|
||||||
esp_transport_list_add(client->transport_list, ssl, "_ssl"); // need to save to transport list, for cleanup
|
esp_transport_list_add(client->transport_list, ssl, "_ssl"); // need to save to transport list, for cleanup
|
||||||
|
|
||||||
esp_transport_handle_t wss = esp_transport_ws_init(ssl);
|
esp_transport_handle_t wss = esp_transport_ws_init(ssl);
|
||||||
|
@ -95,6 +95,10 @@ typedef struct {
|
|||||||
bool disable_pingpong_discon; /*!< Disable auto-disconnect due to no PONG received within pingpong_timeout_sec */
|
bool disable_pingpong_discon; /*!< Disable auto-disconnect due to no PONG received within pingpong_timeout_sec */
|
||||||
bool use_global_ca_store; /*!< Use a global ca_store for all the connections in which this bool is set. */
|
bool use_global_ca_store; /*!< Use a global ca_store for all the connections in which this bool is set. */
|
||||||
bool skip_cert_common_name_check;/*!< Skip any validation of server certificate CN field */
|
bool skip_cert_common_name_check;/*!< Skip any validation of server certificate CN field */
|
||||||
|
bool keep_alive_enable; /*!< Enable keep-alive timeout */
|
||||||
|
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 */
|
||||||
} esp_websocket_client_config_t;
|
} esp_websocket_client_config_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user