mirror of
				https://github.com/espressif/esp-idf.git
				synced 2025-11-04 06:11:06 +00:00 
			
		
		
		
	esp_http_server: Add httpd_ws_get_fd_info() API to check active WS clients
Added a new API to WebSocket server to test provided socket descriptor if it belongs to active clients for this server and if websocket handshake has been performed Closes https://github.com/espressif/esp-idf/issues/5602
This commit is contained in:
		@@ -1586,6 +1586,19 @@ esp_err_t httpd_ws_send_frame(httpd_req_t *req, httpd_ws_frame_t *pkt);
 | 
				
			|||||||
 */
 | 
					 */
 | 
				
			||||||
esp_err_t httpd_ws_send_frame_async(httpd_handle_t hd, int fd, httpd_ws_frame_t *frame);
 | 
					esp_err_t httpd_ws_send_frame_async(httpd_handle_t hd, int fd, httpd_ws_frame_t *frame);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @brief Checks the supplied socket descriptor if it belongs to any active client
 | 
				
			||||||
 | 
					 * of this server instance and if the websoket protocol is active
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @param[in] hd      Server instance data
 | 
				
			||||||
 | 
					 * @param[in] fd      Socket descriptor
 | 
				
			||||||
 | 
					 * @return
 | 
				
			||||||
 | 
					 *  - -1                    : This fd is not a client of this httpd
 | 
				
			||||||
 | 
					 *  - 0                     : This fd is an active client, protocol is not WS
 | 
				
			||||||
 | 
					 *  - 1                     : This fd is an active client, protocol is WS
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					int httpd_ws_get_fd_info(httpd_handle_t hd, int fd)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif /* CONFIG_HTTPD_WS_SUPPORT */
 | 
					#endif /* CONFIG_HTTPD_WS_SUPPORT */
 | 
				
			||||||
/** End of WebSocket related stuff
 | 
					/** End of WebSocket related stuff
 | 
				
			||||||
 * @}
 | 
					 * @}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -383,4 +383,15 @@ esp_err_t httpd_ws_get_frame_type(httpd_req_t *req)
 | 
				
			|||||||
    return ESP_OK;
 | 
					    return ESP_OK;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int 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;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    bool is_active_ws = sess->ws_handshake_done && (!sess->ws_close);
 | 
				
			||||||
 | 
					    return is_active_ws ? 1 : 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif /* CONFIG_HTTPD_WS_SUPPORT */
 | 
					#endif /* CONFIG_HTTPD_WS_SUPPORT */
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user