From a1d5cfc26023c64e973d5b891677a53302e993a0 Mon Sep 17 00:00:00 2001 From: yuanjm Date: Fri, 5 Mar 2021 15:33:02 +0800 Subject: [PATCH] example: Identify the callback whether is hanshake or frame-receive by req->method Now the uri handler gets called immediately after the handshake. In the handler we can identify that this was the handshake by checking req->method as it is still a GET from the first part of the handshake the client has sent. On a normal websocket-frame-receive (when normally a websocket uriHandler gets called) this field is set to 0 Closes https://github.com/espressif/esp-idf/issues/6597 --- .../http_server/ws_echo_server/main/ws_echo_server.c | 4 ++++ .../https_server/wss_server/main/wss_server_example.c | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/examples/protocols/http_server/ws_echo_server/main/ws_echo_server.c b/examples/protocols/http_server/ws_echo_server/main/ws_echo_server.c index b36dcdeee6..03e810f73d 100644 --- a/examples/protocols/http_server/ws_echo_server/main/ws_echo_server.c +++ b/examples/protocols/http_server/ws_echo_server/main/ws_echo_server.c @@ -67,6 +67,10 @@ static esp_err_t trigger_async_send(httpd_handle_t handle, httpd_req_t *req) */ static esp_err_t echo_handler(httpd_req_t *req) { + if (req->method == HTTP_GET) { + ESP_LOGI(TAG, "Handshake done, the new connection was opened"); + return ESP_OK; + } httpd_ws_frame_t ws_pkt; memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t)); ws_pkt.type = HTTPD_WS_TYPE_TEXT; diff --git a/examples/protocols/https_server/wss_server/main/wss_server_example.c b/examples/protocols/https_server/wss_server/main/wss_server_example.c index c1e1512c4c..5286bbe2d9 100644 --- a/examples/protocols/https_server/wss_server/main/wss_server_example.c +++ b/examples/protocols/https_server/wss_server/main/wss_server_example.c @@ -33,6 +33,10 @@ static const size_t max_clients = 4; static esp_err_t ws_handler(httpd_req_t *req) { + if (req->method == HTTP_GET) { + ESP_LOGI(TAG, "Handshake done, the new connection was opened"); + return ESP_OK; + } httpd_ws_frame_t ws_pkt; memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t));