Skip to content

Commit 09f240c

Browse files
committed
doc/websocket: updates the API reference for ESP WebSocket Client
1 parent 79a5b0b commit 09f240c

File tree

1 file changed

+76
-19
lines changed

1 file changed

+76
-19
lines changed

docs/en/api-reference/protocols/esp_websocket_client.rst

Lines changed: 76 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ ESP WebSocket Client
33

44
Overview
55
--------
6-
The ESP WebSocket client is an implementation of `WebSocket protocol client <https://tools.ietf.org/html/rfc6455>`_ for ESP32
6+
The ESP WebSocket client is an implementation of `WebSocket protocol client <https://tools.ietf.org/html/rfc6455>`_ for {IDF_TARGET_NAME}
77

88
Features
99
--------
10-
* supports WebSocket over TCP, SSL with mbedtls
10+
* Supports WebSocket over TCP, TLS with mbedtls
1111
* Easy to setup with URI
1212
* Multiple instances (Multiple clients in one application)
1313

@@ -19,48 +19,105 @@ URI
1919
- Supports ``ws``, ``wss`` schemes
2020
- WebSocket samples:
2121

22-
- ``ws://websocket.org``: WebSocket over TCP, default port 80
23-
- ``wss://websocket.org``: WebSocket over SSL, default port 443
24-
25-
- Minimal configurations:
22+
- ``ws://echo.websocket.org``: WebSocket over TCP, default port 80
23+
- ``wss://echo.websocket.org``: WebSocket over SSL, default port 443
24+
25+
Minimal configurations:
2626

2727
.. code:: c
2828
2929
const esp_websocket_client_config_t ws_cfg = {
30-
.uri = "ws://websocket.org",
30+
.uri = "ws://echo.websocket.org",
3131
};
3232
33-
- If there are any options related to the URI in
34-
``esp_websocket_client_config_t``, the option defined by the URI will be
35-
overridden. Sample:
33+
The WebSocket client supports the use of both path and query in the URI. Sample:
34+
35+
.. code:: c
36+
37+
const esp_websocket_client_config_t ws_cfg = {
38+
.uri = "ws://echo.websocket.org/connectionhandler?id=104",
39+
};
40+
41+
42+
If there are any options related to the URI in
43+
:cpp:type:`esp_websocket_client_config_t`, the option defined by the URI will be
44+
overridden. Sample:
3645

3746
.. code:: c
3847
3948
const esp_websocket_client_config_t ws_cfg = {
40-
.uri = "ws://websocket.org:123",
49+
.uri = "ws://echo.websocket.org:123",
4150
.port = 4567,
4251
};
4352
//WebSocket client will connect to websocket.org using port 4567
4453
45-
SSL
54+
TLS
4655
^^^
4756

48-
- Get certificate from server, example: ``websocket.org``
49-
``openssl s_client -showcerts -connect websocket.org:443 </dev/null 2>/dev/null|openssl x509 -outform PEM >websocket_org.pem``
50-
- Configuration:
57+
Configuration:
5158

52-
.. code:: cpp
59+
.. code:: c
5360
5461
const esp_websocket_client_config_t ws_cfg = {
55-
.uri = "wss://websocket.org",
62+
.uri = "wss://echo.websocket.org",
5663
.cert_pem = (const char *)websocket_org_pem_start,
5764
};
5865
59-
For more options on ``esp_websocket_client_config_t``, please refer to API reference below
66+
.. note:: If you want to verify the server, then you need to provide a certificate in PEM format, and provide to ``cert_pem`` in :cpp:type:`websocket_client_config_t`. If no certficate is provided then the TLS connection will to default not requiring verification.
67+
68+
PEM certificate for this example could be extracted from an openssl `s_client` command connecting to websocket.org.
69+
In case a host operating system has `openssl` and `sed` packages installed, one could execute the following command to download and save the root or intermediate root certificate to a file (Note for Windows users: Both Linux like environment or Windows native packages may be used).
70+
```
71+
echo "" | openssl s_client -showcerts -connect websocket.org:443 | sed -n "1,/Root/d; /BEGIN/,/END/p" | openssl x509 -outform PEM >websocket_org.pem
72+
```
73+
74+
This command will extract the second certificate in the chain and save it as a pem-file.
75+
76+
Subprotocol
77+
^^^^^^^^^^^
78+
79+
The subprotocol field in the config struct can be used to request a subprotocol
80+
81+
.. code:: c
82+
83+
const esp_websocket_client_config_t ws_cfg = {
84+
.uri = "ws://websocket.org",
85+
.subprotocol = "soap",
86+
};
87+
88+
.. note:: The client is indifferent to the subprotocol field in the server response and will accept the connection no matter what the server replies.
89+
90+
For more options on :cpp:type:`esp_websocket_client_config_t`, please refer to API reference below
91+
92+
Events
93+
------
94+
* `WEBSOCKET_EVENT_CONNECTED`: The client has successfully established a connection to the server. The client is now ready to send and receive data. Contains no event data.
95+
* `WEBSOCKET_EVENT_DISCONNECTED`: The client has aborted the connection due to the transport layer failing to read data, e.g. because the server is unavailable. Contains no event data.
96+
* `WEBSOCKET_EVENT_DATA`: The client has successfully received and parsed a WebSocket frame. The event data contains a pointer to the payload data, the length of the payload data as well as the opcode of the received frame. A message may be fragmented into multiple events if the length exceeds the buffer size. This event will also be posted for non-payload frames, e.g. pong or connection close frames.
97+
* `WEBSOCKET_EVENT_ERROR`: Not used in the current implementation of the client.
98+
99+
If the client handle is needed in the event handler it can be accessed through the pointer passed to the event handler:
100+
101+
.. code:: c
102+
103+
esp_websocket_client_handle_t client = (esp_websocket_client_handle_t)handler_args;
104+
105+
106+
Limitations and Known Issues
107+
----------------------------
108+
* The client is able to request the use of a subprotocol from the server during the handshake, but does not do any subprotocol related checks on the response from the server.
60109

61110
Application Example
62111
-------------------
63-
Simple WebSocket example that uses esp_websocket_client to establish a websocket connection and send/receive data with the `websocket.org <https://websocket.org>`_ Server: :example:`protocols/websocket`.
112+
A simple WebSocket example that uses esp_websocket_client to establish a websocket connection and send/receive data with the `websocket.org <https://websocket.org>`_ server can be found here: :example:`protocols/websocket`.
113+
114+
Sending Text Data
115+
^^^^^^^^^^^^^^^^^
116+
The WebSocket client supports sending data as a text data frame, which informs the application layer that the payload data is text data encoded as UTF-8. Example:
117+
118+
.. code:: cpp
119+
120+
esp_websocket_client_send_text(client, data, len, portMAX_DELAY);
64121
65122
66123
API Reference

0 commit comments

Comments
 (0)