Merge branch 'feat/improve_ws_server_handshake_handling_v6.0' into 'release/v6.0'

feat(http_server): improve websocket server handling (v6.0)

See merge request espressif/esp-idf!46247
This commit is contained in:
Jiang Jiang Jian
2026-03-20 10:38:23 +08:00
8 changed files with 114 additions and 16 deletions
@@ -78,6 +78,15 @@ The pre-handshake callback can be used for authentication, authorization, or oth
To use the WebSocket pre-handshake callback, you must enable :ref:`CONFIG_HTTPD_WS_PRE_HANDSHAKE_CB_SUPPORT` in your project configuration.
WebSocket Post-Handshake Callback
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Similar to the pre-handshake callback, the HTTP server component also provides a post-handshake callback for WebSocket endpoints. This callback is invoked after the WebSocket handshake is processed.
At this point the connection has been upgraded to WebSocket, and the server has responded with the WebSocket handshake response. This post handshake callback can be used for logging, sending initial messages, or other setup tasks.
To use the WebSocket post-handshake callback, you must enable :ref:`CONFIG_HTTPD_WS_POST_HANDSHAKE_CB_SUPPORT` in your project configuration.
.. code-block:: c
static esp_err_t ws_auth_handler(httpd_req_t *req)
@@ -78,6 +78,15 @@ HTTP 服务器组件为 WebSocket 端点提供了握手前回调 (pre-handshake
要使用 WebSocket 握手前回调,需在项目配置中启用 :ref:`CONFIG_HTTPD_WS_PRE_HANDSHAKE_CB_SUPPORT` 选项。
WebSocket 握手后回调
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
与握手前回调类似,HTTP 服务器组件也为 WebSocket 端点提供了握手后回调。该回调会在处理完 WebSocket 握手后被调用。
此时连接已升级为 WebSocket,服务器已返回 WebSocket 握手响应。该握手后回调可用于记录日志、发送初始消息或执行其他初始化任务。
要使用 WebSocket 握手后回调功能,需在项目配置中启用 :ref:`CONFIG_HTTPD_WS_POST_HANDSHAKE_CB_SUPPORT` 选项。
.. code-block:: c
static esp_err_t ws_auth_handler(httpd_req_t *req)