mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-13 13:50:21 +00:00
esp_http_server: Add Websocket API to return list of active clients
Closes https://github.com/espressif/esp-idf/issues/5406
This commit is contained in:
@@ -104,6 +104,25 @@ esp_err_t httpd_queue_work(httpd_handle_t handle, httpd_work_fn_t work, void *ar
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t httpd_get_client_list(httpd_handle_t handle, size_t max_fds, httpd_client_list_t *fd_list)
|
||||
{
|
||||
struct httpd_data *hd = (struct httpd_data *) handle;
|
||||
if (hd == NULL || max_fds == 0 || fd_list == NULL || max_fds < hd->config.max_open_sockets) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
fd_list->active_clients = 0;
|
||||
for (int i = 0; i < hd->config.max_open_sockets; ++i) {
|
||||
if (hd->hd_sd[i].fd != -1) {
|
||||
if (fd_list->active_clients < max_fds) {
|
||||
fd_list->client_fds[fd_list->active_clients++] = hd->hd_sd[i].fd;
|
||||
} else {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void *httpd_get_global_user_ctx(httpd_handle_t handle)
|
||||
{
|
||||
return ((struct httpd_data *)handle)->config.global_user_ctx;
|
||||
|
@@ -758,8 +758,8 @@ esp_err_t httpd_req_new(struct httpd_data *hd, struct sock_db *sd)
|
||||
sd->ws_handler != NULL ? "Yes" : "No",
|
||||
sd->ws_close ? "Yes" : "No");
|
||||
if (sd->ws_handshake_done && sd->ws_handler != NULL) {
|
||||
ESP_LOGD(TAG, LOG_FMT("New WS request from existing socket"));
|
||||
ret = httpd_ws_get_frame_type(r);
|
||||
ESP_LOGD(TAG, LOG_FMT("New WS request from existing socket, ws_type=%d"), ra->ws_type);
|
||||
|
||||
/* Stop and return here immediately if it's a CLOSE frame */
|
||||
if (ra->ws_type == HTTPD_WS_TYPE_CLOSE) {
|
||||
@@ -767,13 +767,13 @@ esp_err_t httpd_req_new(struct httpd_data *hd, struct sock_db *sd)
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Ignore PONG frame, as this is a server */
|
||||
if (ra->ws_type == HTTPD_WS_TYPE_PONG) {
|
||||
return ret;
|
||||
/* Pass the PONG frames to the handler as well, as user app might send PINGs */
|
||||
ESP_LOGD(TAG, LOG_FMT("Received PONG frame"));
|
||||
}
|
||||
|
||||
/* Call handler if it's a non-control frame */
|
||||
if (ret == ESP_OK && ra->ws_type < HTTPD_WS_TYPE_CLOSE) {
|
||||
if (ret == ESP_OK && ra->ws_type <= HTTPD_WS_TYPE_PONG) {
|
||||
ret = sd->ws_handler(r);
|
||||
}
|
||||
|
||||
|
@@ -383,15 +383,15 @@ esp_err_t httpd_ws_get_frame_type(httpd_req_t *req)
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
int httpd_ws_get_fd_info(httpd_handle_t hd, int fd)
|
||||
httpd_ws_client_info_t httpd_ws_get_fd_info(httpd_handle_t hd, int fd)
|
||||
{
|
||||
struct sock_db *sess = httpd_sess_get(hd, fd);
|
||||
|
||||
if (sess == NULL) {
|
||||
return -1;
|
||||
return HTTPD_WS_CLIENT_INVALID;
|
||||
}
|
||||
bool is_active_ws = sess->ws_handshake_done && (!sess->ws_close);
|
||||
return is_active_ws ? 1 : 0;
|
||||
return is_active_ws ? HTTPD_WS_CLIENT_WEBSOCKET : HTTPD_WS_CLIENT_HTTP;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_HTTPD_WS_SUPPORT */
|
||||
|
Reference in New Issue
Block a user