|
| 1 | +# WebSocket Feature Showcase Example |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This example demonstrates the newest `esp_websocket_client` capabilities in one place, with verbose comments in code and runtime logs. |
| 6 | + |
| 7 | +It is designed to be a **reference app** when you want to copy/paste a production-ready websocket control flow and then trim it for your use case. |
| 8 | + |
| 9 | +### Features Demonstrated |
| 10 | + |
| 11 | +1. Event-driven lifecycle handling (`BEGIN`, `BEFORE_CONNECT`, `CONNECTED`, `DATA`, `ERROR`, `DISCONNECTED`, `CLOSED`, `FINISH`) |
| 12 | +2. Header inspection via `WEBSOCKET_EVENT_HEADER_RECEIVED` (IDF 6+) |
| 13 | +3. Custom headers using both: |
| 14 | + - `esp_websocket_client_set_headers()` |
| 15 | + - `esp_websocket_client_append_header()` |
| 16 | +4. Fragmented transfer APIs: |
| 17 | + - `esp_websocket_client_send_text_partial()` |
| 18 | + - `esp_websocket_client_send_bin_partial()` |
| 19 | + - `esp_websocket_client_send_cont_msg()` |
| 20 | + - `esp_websocket_client_send_fin()` |
| 21 | +5. Runtime reconnect tuning: |
| 22 | + - `esp_websocket_client_get_reconnect_timeout()` |
| 23 | + - `esp_websocket_client_set_reconnect_timeout()` |
| 24 | +6. Runtime ping tuning: |
| 25 | + - `esp_websocket_client_get_ping_interval_sec()` |
| 26 | + - `esp_websocket_client_set_ping_interval_sec()` |
| 27 | +7. Task-preserving reconnect with: |
| 28 | + - `esp_websocket_client_pause()` |
| 29 | + - `esp_websocket_client_set_uri()` |
| 30 | + - `esp_websocket_client_resume()` |
| 31 | +8. Close handshake and clean teardown: |
| 32 | + - `esp_websocket_client_close()` |
| 33 | + - unregister events |
| 34 | + - `esp_websocket_client_destroy()` |
| 35 | +9. Heap/stability observability: |
| 36 | + - heap snapshots before init and after destroy |
| 37 | + - lifecycle summary logging for connect/error states |
| 38 | + - cleanup path that avoids hard-failing during recovery paths |
| 39 | + |
| 40 | +## Project Layout |
| 41 | + |
| 42 | +- `main/app_main.c` — fully commented example source. |
| 43 | +- `main/Kconfig.projbuild` — URI configuration in menuconfig. |
| 44 | +- `main/idf_component.yml` — dependencies. |
| 45 | + |
| 46 | +## Configure |
| 47 | + |
| 48 | +Set your endpoint in menuconfig: |
| 49 | + |
| 50 | +```bash |
| 51 | +idf.py menuconfig |
| 52 | +``` |
| 53 | + |
| 54 | +Path: |
| 55 | + |
| 56 | +```text |
| 57 | +WebSocket Feature Showcase Configuration ---> |
| 58 | + WebSocket endpoint URI |
| 59 | +``` |
| 60 | + |
| 61 | +Default endpoint: |
| 62 | + |
| 63 | +```text |
| 64 | +wss://echo.websocket.events |
| 65 | +``` |
| 66 | + |
| 67 | +## Build & Flash |
| 68 | + |
| 69 | +```bash |
| 70 | +idf.py set-target esp32 |
| 71 | +idf.py build |
| 72 | +idf.py -p /dev/ttyUSB0 flash monitor |
| 73 | +``` |
| 74 | + |
| 75 | +## What to look for in logs |
| 76 | + |
| 77 | +- Upgrade headers printed by `WEBSOCKET_EVENT_HEADER_RECEIVED`. |
| 78 | +- Text + binary + fragmented send sequences. |
| 79 | +- Ping/reconnect values before and after runtime update. |
| 80 | +- Pause/resume flow reconnecting without destroying the task. |
| 81 | +- Heap snapshots and a final "Heap recovery" message after client destroy. |
| 82 | + |
| 83 | +## Notes |
| 84 | + |
| 85 | +- If your server uses TLS with a private CA, configure certificates as needed for your deployment. |
| 86 | +- Redirect handling is managed by the websocket transport layer; this example focuses on application-side controls and observability. |
0 commit comments