Skip to content

Commit 9da66bf

Browse files
committed
feat(websocket): enhance example with docs, testing tools, and standalone server
- Add comprehensive README with TOC and quick start - Add pytest setup and certificate generation scripts - Add standalone WebSocket test server with TLS support - Add troubleshooting and multiple testing approaches
1 parent 6f6237a commit 9da66bf

File tree

5 files changed

+370
-82
lines changed

5 files changed

+370
-82
lines changed

components/esp_websocket_client/examples/target/README.md

Lines changed: 182 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,42 @@
11
# Websocket Sample application
22

3-
This example will shows how to set up and communicate over a websocket.
3+
This example shows how to set up and communicate over a websocket.
4+
5+
## Table of Contents
6+
7+
- [Hardware Required](#hardware-required)
8+
- [Configure the project](#configure-the-project)
9+
- [Pre-configured SDK Configurations](#pre-configured-sdk-configurations)
10+
- [Server Certificate Verification](#server-certificate-verification)
11+
- [Generating Self-signed Certificates](#generating-a-self-signed-certificates-with-openssl)
12+
- [Build and Flash](#build-and-flash)
13+
- [Testing with pytest](#testing-with-pytest)
14+
- [Example Output](#example-output)
15+
- [WebSocket Test Server](#websocket-test-server)
16+
- [Python Flask Echo Server](#alternative-python-flask-echo-server)
17+
18+
## Quick Start
19+
20+
1. **Install dependencies:**
21+
```bash
22+
pip install -r esp-protocols/ci/requirements.txt
23+
```
24+
25+
2. **Configure and build:**
26+
```bash
27+
idf.py menuconfig # Configure WiFi/Ethernet and WebSocket URI
28+
idf.py build
29+
```
30+
31+
3. **Flash and monitor:**
32+
```bash
33+
idf.py -p PORT flash monitor
34+
```
35+
36+
4. **Run tests:**
37+
```bash
38+
pytest .
39+
```
440

541
## How to Use Example
642

@@ -15,6 +51,27 @@ This example can be executed on any ESP32 board, the only required interface is
1551
* Configure the websocket endpoint URI under "Example Configuration", if "WEBSOCKET_URI_FROM_STDIN" is selected then the example application will connect to the URI it reads from stdin (used for testing)
1652
* To test a WebSocket client example over TLS, please enable one of the following configurations: `CONFIG_WS_OVER_TLS_MUTUAL_AUTH` or `CONFIG_WS_OVER_TLS_SERVER_AUTH`. See the sections below for more details.
1753

54+
### Pre-configured SDK Configurations
55+
56+
This example includes several pre-configured `sdkconfig.ci.*` files for different testing scenarios:
57+
58+
* **sdkconfig.ci** - Default configuration with WebSocket over Ethernet (IP101 PHY, ESP32, IPv6) and hardcoded URI.
59+
* **sdkconfig.ci.plain_tcp** - WebSocket over plain TCP (no TLS, URI from stdin) using Ethernet (IP101 PHY, ESP32, IPv6).
60+
* **sdkconfig.ci.mutual_auth** - WebSocket with mutual TLS authentication (client/server certificate verification, skips CN check) and URI from stdin.
61+
* **sdkconfig.ci.dynamic_buffer** - WebSocket with dynamic buffer allocation, Ethernet (IP101 PHY, ESP32, IPv6), and hardcoded URI.
62+
63+
To use a specific configuration for building:
64+
```
65+
rm sdkconfig
66+
cp sdkconfig.ci.plain_tcp sdkconfig
67+
idf.py build
68+
```
69+
70+
Or specify it directly when building:
71+
```
72+
idf.py -DSDKCONFIG_DEFAULTS="sdkconfig.ci.plain_tcp" build
73+
```
74+
1875
### Server Certificate Verification
1976

2077
* Mutual Authentication: When `CONFIG_WS_OVER_TLS_MUTUAL_AUTH=y` is enabled, it's essential to provide valid certificates for both the server and client.
@@ -26,7 +83,7 @@ This example can be executed on any ESP32 board, the only required interface is
2683
Please note: This example represents an extremely simplified approach to generating self-signed certificates/keys with a single common CA, devoid of CN checks, lacking password protection, and featuring hardcoded key sizes and types. It is intended solely for testing purposes.
2784
In the outlined steps, we are omitting the configuration of the CN (Common Name) field due to the context of a testing environment. However, it's important to recognize that the CN field is a critical element of SSL/TLS certificates, significantly influencing the security and efficacy of HTTPS communications. This field facilitates the verification of a website's identity, enhancing trust and security in web interactions. In practical deployments beyond testing scenarios, ensuring the CN field is accurately set is paramount for maintaining the integrity and reliability of secure communications
2885

29-
### Generating a self signed Certificates with OpenSSL
86+
### Generating a self signed Certificates with OpenSSL manually
3087
* The example below outlines the process for creating new certificates for both the server and client using OpenSSL, a widely-used command line tool for implementing TLS protocol:
3188

3289
```
@@ -63,6 +120,26 @@ Please see the openssl man pages (man openssl) for more details.
63120
It is **strongly recommended** to not reuse the example certificate in your application;
64121
it is included only for demonstration.
65122

123+
### Certificate Generation Options
124+
125+
#### Option 1: Manual OpenSSL Commands
126+
Follow the step-by-step process in the section above to understand certificate generation.
127+
128+
#### Option 2: Automated Script
129+
**Note:** Test certificates are already available in the example. If you want to regenerate them or create new ones, use the provided `generate_certs.sh` script:
130+
131+
```bash
132+
chmod +x generate_certs.sh
133+
./generate_certs.sh
134+
```
135+
136+
This script automatically generates all required certificates in the correct directories and cleans up temporary files.
137+
138+
#### Option 3: Online Certificate Generators
139+
- **mkcert**: `install mkcert` then `mkcert -install` and `mkcert localhost`
140+
- **Let's Encrypt**: For production certificates (free, automated renewal)
141+
- **Online generators**: Search for "self-signed certificate generator" online
142+
66143
### Build and Flash
67144

68145
Build the project and flash it to the board, then run monitor tool to view serial output:
@@ -73,7 +150,36 @@ idf.py -p PORT flash monitor
73150

74151
(To exit the serial monitor, type ``Ctrl-]``.)
75152

76-
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
153+
See the [ESP-IDF Getting Started Guide](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/index.html) for full steps to configure and use ESP-IDF to build projects.
154+
155+
## Testing with pytest
156+
157+
### Install Dependencies
158+
159+
Before running the pytest tests, you need to install the required Python packages:
160+
161+
```
162+
pip install -r esp-protocols/ci/requirements.txt
163+
```
164+
165+
### Run pytest
166+
167+
After installing the dependencies, you can run the pytest tests:
168+
169+
Run all tests in current directory:
170+
```
171+
pytest .
172+
```
173+
174+
Run specific test file:
175+
```
176+
pytest pytest_websocket.py
177+
```
178+
179+
To specify the target device or serial port, use:
180+
```
181+
pytest --target esp32 --port /dev/ttyUSB0
182+
```
77183

78184
## Example Output
79185

@@ -105,9 +211,51 @@ W (9162) WEBSOCKET: Received=hello 0003
105211
```
106212

107213

108-
## Python Flask echo server
214+
## WebSocket Test Server
215+
216+
### Standalone Test Server
217+
218+
This example includes a standalone WebSocket test server (`websocket_server.py`) that can be used for testing your ESP32 WebSocket client:
219+
220+
#### Quick Start
221+
```bash
222+
# Plain WebSocket server
223+
python3 websocket_server.py
224+
225+
# TLS WebSocket server
226+
python3 websocket_server.py --tls
227+
228+
# TLS with client certificate verification
229+
python3 websocket_server.py --tls --client-verify
230+
231+
# Custom port
232+
python3 websocket_server.py --port 9000 --tls
233+
```
234+
235+
#### Server Features
236+
- **Echo functionality** - Automatically echoes back received messages
237+
- **TLS support** - Secure WebSocket (WSS) connections
238+
- **Client certificate verification** - Mutual authentication support
239+
- **Binary and text messages** - Handles both data types
240+
- **Auto IP detection** - Shows connection URL with your local IP
241+
242+
#### Usage Examples
243+
```bash
244+
# Basic server (port 8080)
245+
python3 websocket_server.py
109246

110-
By default, the `wss://echo.websocket.org` endpoint is used. You can setup a Python websocket echo server locally and try the `ws://<your-ip>:5000` endpoint. To do this, install Flask-sock Python package
247+
# TLS server with client verification
248+
python3 websocket_server.py --tls --client-verify
249+
250+
# Custom port with TLS
251+
python3 websocket_server.py --port 9000 --tls
252+
```
253+
254+
The server will display the connection URL (e.g., `wss://192.168.1.100:8080`) that you can use in your ESP32 configuration.
255+
256+
### Alternative: Python Flask Echo Server
257+
258+
By default, the `wss://echo.websocket.org` endpoint is used. You can also setup a Python Flask websocket echo server locally and try the `ws://<your-ip>:5000` endpoint. To do this, install Flask-sock Python package
111259

112260
```
113261
pip install flask-sock
@@ -135,3 +283,32 @@ if __name__ == '__main__':
135283
# gunicorn -b 0.0.0.0:5000 --workers 4 --threads 100 module:app
136284
app.run(host="0.0.0.0", debug=True)
137285
```
286+
287+
## Troubleshooting
288+
289+
### Common Issues
290+
291+
**Connection failed:**
292+
- Verify WiFi/Ethernet configuration in `idf.py menuconfig`
293+
- Check if the WebSocket server is running and accessible
294+
- Ensure the URI is correct (use `wss://` for TLS, `ws://` for plain TCP)
295+
296+
**TLS certificate errors:**
297+
- For self-signed certificates, ensure `CONFIG_WS_OVER_TLS_SKIP_COMMON_NAME_CHECK=y` is enabled
298+
- Verify certificate files are properly formatted and accessible
299+
300+
**Build errors:**
301+
- Clean build: `idf.py fullclean`
302+
- Check ESP-IDF version compatibility
303+
- Verify all dependencies are installed
304+
305+
**Test failures:**
306+
- Ensure the device is connected and accessible via the specified port
307+
- Check that the target device matches the configuration (`--target esp32`)
308+
- Verify pytest dependencies are installed correctly
309+
310+
### Getting Help
311+
312+
- Check the [ESP-IDF documentation](https://docs.espressif.com/projects/esp-idf/)
313+
- Review the [WebSocket client component documentation](../../README.md)
314+
- Report issues on the [ESP Protocols repository](https://github.com/espressif/esp-protocols)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
# Generate CA, Server, and Client certificates automatically
3+
4+
# Create directories if they don't exist
5+
mkdir -p main/certs/server
6+
7+
echo "Generating CA certificate..."
8+
openssl genrsa -out main/certs/ca_key.pem 2048
9+
openssl req -new -x509 -days 3650 -key main/certs/ca_key.pem -out main/certs/ca_cert.pem -subj "/C=US/ST=State/L=City/O=Organization/CN=TestCA"
10+
11+
echo "Generating Server certificate..."
12+
openssl genrsa -out main/certs/server/server_key.pem 2048
13+
openssl req -new -key main/certs/server/server_key.pem -out server_csr.pem -subj "/C=US/ST=State/L=City/O=Organization/CN=localhost"
14+
openssl x509 -req -days 3650 -in server_csr.pem -CA main/certs/ca_cert.pem -CAkey main/certs/ca_key.pem -CAcreateserial -out main/certs/server/server_cert.pem
15+
16+
echo "Generating Client certificate..."
17+
openssl genrsa -out main/certs/client_key.pem 2048
18+
openssl req -new -key main/certs/client_key.pem -out client_csr.pem -subj "/C=US/ST=State/L=City/O=Organization/CN=TestClient"
19+
openssl x509 -req -days 3650 -in client_csr.pem -CA main/certs/ca_cert.pem -CAkey main/certs/ca_key.pem -CAcreateserial -out main/certs/client_cert.pem
20+
21+
# Clean up CSR files
22+
rm server_csr.pem client_csr.pem
23+
24+
echo "Certificates generated successfully!"
25+
echo "Generated files:"
26+
echo " - main/certs/ca_cert.pem (CA certificate)"
27+
echo " - main/certs/ca_key.pem (CA private key)"
28+
echo " - main/certs/client_cert.pem (Client certificate)"
29+
echo " - main/certs/client_key.pem (Client private key)"
30+
echo " - main/certs/server/server_cert.pem (Server certificate)"
31+
echo " - main/certs/server/server_key.pem (Server private key)"

components/esp_websocket_client/examples/target/main/websocket_example.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Unlicense OR CC0-1.0
55
*/
@@ -142,7 +142,11 @@ static void websocket_app_start(void)
142142
#if CONFIG_WEBSOCKET_URI_FROM_STDIN
143143
char line[128];
144144

145-
ESP_LOGI(TAG, "Please enter uri of websocket endpoint");
145+
ESP_LOGI(TAG, "Please enter WebSocket endpoint URI");
146+
ESP_LOGI(TAG, "Examples:");
147+
ESP_LOGI(TAG, " ws://192.168.1.100:8080 (plain WebSocket)");
148+
ESP_LOGI(TAG, " wss://192.168.1.100:8080 (secure WebSocket)");
149+
ESP_LOGI(TAG, " wss://echo.websocket.org (public test server)");
146150
get_string(line, sizeof(line));
147151

148152
websocket_cfg.uri = line;

components/esp_websocket_client/examples/target/pytest_websocket.py

Lines changed: 3 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,12 @@
1-
# SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
1+
# SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
22
# SPDX-License-Identifier: Unlicense OR CC0-1.0
33
import json
44
import random
55
import re
6-
import socket
7-
import ssl
86
import string
97
import sys
10-
from threading import Event, Thread
118

12-
from SimpleWebSocketServer import (SimpleSSLWebSocketServer,
13-
SimpleWebSocketServer, WebSocket)
14-
15-
16-
def get_my_ip():
17-
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
18-
try:
19-
# doesn't even have to be reachable
20-
s.connect(('8.8.8.8', 1))
21-
IP = s.getsockname()[0]
22-
except Exception:
23-
IP = '127.0.0.1'
24-
finally:
25-
s.close()
26-
return IP
27-
28-
29-
class WebsocketTestEcho(WebSocket):
30-
def handleMessage(self):
31-
if isinstance(self.data, bytes):
32-
print(f'\n Server received binary data: {self.data.hex()}\n')
33-
self.sendMessage(self.data, binary=True)
34-
else:
35-
print(f'\n Server received: {self.data}\n')
36-
self.sendMessage(self.data)
37-
38-
def handleConnected(self):
39-
print('Connection from: {}'.format(self.address))
40-
41-
def handleClose(self):
42-
print('{} closed the connection'.format(self.address))
43-
44-
45-
# Simple Websocket server for testing purposes
46-
class Websocket(object):
47-
48-
def send_data(self, data):
49-
for nr, conn in self.server.connections.items():
50-
conn.sendMessage(data)
51-
52-
def run(self):
53-
if self.use_tls is True:
54-
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
55-
ssl_context.load_cert_chain(certfile='main/certs/server/server_cert.pem', keyfile='main/certs/server/server_key.pem')
56-
if self.client_verify is True:
57-
ssl_context.load_verify_locations(cafile='main/certs/ca_cert.pem')
58-
ssl_context.verify_mode = ssl.CERT_REQUIRED
59-
ssl_context.check_hostname = False
60-
self.server = SimpleSSLWebSocketServer('', self.port, WebsocketTestEcho, ssl_context=ssl_context)
61-
else:
62-
self.server = SimpleWebSocketServer('', self.port, WebsocketTestEcho)
63-
while not self.exit_event.is_set():
64-
self.server.serveonce()
65-
66-
def __init__(self, port, use_tls, verify):
67-
self.port = port
68-
self.use_tls = use_tls
69-
self.client_verify = verify
70-
self.exit_event = Event()
71-
self.thread = Thread(target=self.run)
72-
self.thread.start()
73-
74-
def __enter__(self):
75-
return self
76-
77-
def __exit__(self, exc_type, exc_value, traceback):
78-
self.exit_event.set()
79-
self.thread.join(10)
80-
if self.thread.is_alive():
81-
print('Thread cannot be joined', 'orange')
9+
from websocket_server import WebsocketServer, get_my_ip
8210

8311

8412
def test_examples_protocol_websocket(dut):
@@ -228,7 +156,7 @@ def test_fragmented_binary_msg(dut):
228156

229157
if uri_from_stdin:
230158
server_port = 8080
231-
with Websocket(server_port, use_tls, client_verify) as ws:
159+
with WebsocketServer(server_port, use_tls, client_verify) as ws:
232160
if use_tls is True:
233161
uri = 'wss://{}:{}'.format(get_my_ip(), server_port)
234162
else:

0 commit comments

Comments
 (0)