How to send HTML page from a Web Server asynchronously in ESP-IDF?

is it possible to send the HTML page from a Web Server in async mode upon initial client request? tried that and all i get in the browser is the raw HTML script maybe I am using the wrong function :
esp_err_t httpd_ws_send_data_async(httpd_handle_t handle, int socket, httpd_ws_frame_t *frame,transfer_complete_cb callback, void *arg);
esp_err_t httpd_ws_send_data_async(httpd_handle_t handle, int socket, httpd_ws_frame_t *frame,transfer_complete_cb callback, void *arg);
Solution:
In the ESP-IDF's HTTP server component, the httpd_start() function sets up both a listening TCP socket for handling HTTP traffic and a control UDP socket for internal control signals. However, for serving HTTP responses, including HTML pages, you typically interact with the HTTP request and response structure provided by the server rather than directly managing sockets @te0
Jump to solution
9 Replies
Enthernet Code
Enthernet Code5mo ago
Hi @te0 To serve an HTML page from an ESP32 web server, you should use a standard HTTP response instead of a WebSocket function. The httpd_ws_send_data_async function you mentioned is designed for WebSocket data, not for serving web pages.
Enthernet Code
Enthernet Code5mo ago
To be able to send HTML page upon request you should first create a http handler
esp_err_t html_get_handler(httpd_req_t *req) {
const char *html_content = "<html><body><h1>Hello, World!</h1></body></html>";
httpd_resp_set_type(req, "text/html");
httpd_resp_send(req, html_content, HTTPD_RESP_USE_STRLEN);
return ESP_OK;
}
esp_err_t html_get_handler(httpd_req_t *req) {
const char *html_content = "<html><body><h1>Hello, World!</h1></body></html>";
httpd_resp_set_type(req, "text/html");
httpd_resp_send(req, html_content, HTTPD_RESP_USE_STRLEN);
return ESP_OK;
}
Enthernet Code
Enthernet Code5mo ago
And then setup the sever to call on the handler upon user request
void start_webserver(void) {
httpd_handle_t server = NULL;
httpd_config_t config = HTTPD_DEFAULT_CONFIG();

if (httpd_start(&server, &config) == ESP_OK) {
httpd_uri_t html_uri = {
.uri = "/",
.method = HTTP_GET,
.handler = html_get_handler,
.user_ctx = NULL
};
httpd_register_uri_handler(server, &html_uri);
}
}
void start_webserver(void) {
httpd_handle_t server = NULL;
httpd_config_t config = HTTPD_DEFAULT_CONFIG();

if (httpd_start(&server, &config) == ESP_OK) {
httpd_uri_t html_uri = {
.uri = "/",
.method = HTTP_GET,
.handler = html_get_handler,
.user_ctx = NULL
};
httpd_register_uri_handler(server, &html_uri);
}
}
te0
te05mo ago
Yeap, and the catch is that the HTML page is 4000 bytes long:facepalm:
te0
te05mo ago
I will try to send it in chunks
te0
te05mo ago
I thought about sending back the page with a socket because in the documentation it says :httpd_start(): Creates an instance of HTTP server [...]. The server has both, a listening socket (TCP) for HTTP traffic, and a control socket (UDP) for control signals, which are selected in a round robin fashion in the server task loop
Enthernet Code
Enthernet Code5mo ago
You can use the httpd_resp_send_chunk function for this
Solution
Enthernet Code
Enthernet Code5mo ago
In the ESP-IDF's HTTP server component, the httpd_start() function sets up both a listening TCP socket for handling HTTP traffic and a control UDP socket for internal control signals. However, for serving HTTP responses, including HTML pages, you typically interact with the HTTP request and response structure provided by the server rather than directly managing sockets @te0
te0
te05mo ago
Thanks, it works now, I splitted it up in 2 pieces
Want results from more Discord servers?
Add your server