mirror of
https://github.com/espressif/esp-idf.git
synced 2026-04-27 19:13:21 +00:00
docs: Update CN translation for esp_http_server.rst
This commit is contained in:
@@ -12,7 +12,9 @@ HTTP Server 组件提供了在 ESP32 上运行轻量级 Web 服务器的功能
|
||||
* :cpp:func:`httpd_stop`: 根据传入的句柄停止服务器,并释放相关联的内存和资源。这是一个阻塞函数,首先给服务器任务发送停止信号,然后等待其终止。期间服务器任务会关闭所有已打开的连接,删除已注册的 URI 处理程序,并将所有会话的上下文数据重置为空。
|
||||
* :cpp:func:`httpd_register_uri_handler`: 通过传入 ``httpd_uri_t`` 结构体类型的对象来注册 URI 处理程序。该结构体包含如下成员:``uri`` 名字,``method`` 类型(比如 ``HTTP_GET/HTTP_POST/HTTP_PUT`` 等等), ``esp_err_t *handler (httpd_req_t *req)`` 类型的函数指针,指向用户上下文数据的 ``user_ctx`` 指针。
|
||||
|
||||
.. note:: HTTP Server 组件的 API 并不是线程安全的。如果需要线程安全,应用层需自行确保在多个任务之间进行适当的同步。
|
||||
.. note::
|
||||
|
||||
HTTP Server 组件的 API 并不是线程安全的。如果需要线程安全,应用层需自行确保在多个任务之间进行适当的同步。
|
||||
|
||||
应用示例
|
||||
--------
|
||||
@@ -21,6 +23,33 @@ HTTP Server 组件提供了在 ESP32 上运行轻量级 Web 服务器的功能
|
||||
|
||||
- :example:`protocols/http_server/advanced_tests` 演示了如何使用 HTTP 服务器进行高级测试。
|
||||
|
||||
网络接口绑定
|
||||
------------
|
||||
|
||||
默认情况下,服务器监听所有可用接口 (``INADDR_ANY``)。当 ``httpd_config_t.if_name`` 为 ``NULL`` 时即为此行为。
|
||||
|
||||
若要将 HTTP 服务器绑定到特定的网络接口,请将 ``httpd_config_t.if_name`` 设置为指向 ``struct ifreq`` 结构体,并填充其中的 ``ifr_name`` 字段(例如 ``"eth0"``、``"en0"`` 或 ``"lo"``,具体取决于平台)。
|
||||
|
||||
.. code-block:: c
|
||||
|
||||
#include <net/if.h>
|
||||
|
||||
httpd_handle_t server = NULL;
|
||||
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
||||
|
||||
struct ifreq ifr = {0};
|
||||
strncpy(ifr.ifr_name, "eth0", sizeof(ifr.ifr_name) - 1);
|
||||
ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
|
||||
|
||||
config.if_name = 𝔦
|
||||
config.server_port = 80;
|
||||
|
||||
ESP_ERROR_CHECK(httpd_start(&server, &config));
|
||||
|
||||
注意事项:
|
||||
|
||||
- ``if_name`` 仅在 ``httpd_start()`` 调用期间使用。``ifreq`` 对象只需在该调用期间保持有效即可。
|
||||
|
||||
HTTP 长连接
|
||||
-----------
|
||||
|
||||
|
||||
Reference in New Issue
Block a user