Websocket Server
Overview
This example demonstrates how to implement a simple Websocket Server.
Requirements
A working AP is required.
Configure WiFi SSID , WiFi Password in menuconfig and use security or not.
Building and Flashing
Example Location: examples/network/httpd_websocket/Websocket_server
compile, burn, and more, see: Quick Start Guide
Attention
This example implements a Websocket server with an echo function. When it receives data sent from the client, it will send it back to the client.
The client can use the Postman software to send data to the server implemented in this example.
Certificate
When using https, a certificate is required. The self-signed certificate used in the example main.c is generated by the following commands:
generate a cert which signed by itself:
1 Generate ECDSA private key:
openssl ecparam -genkey -name prime256v1 -out server.key
2 Generate Certificate Signing Request (CSR):
openssl req -new -key server.key -out server.csr
3 Generate self signed certificate (99 Years):
openssl x509 -req -in server.csr -signkey server.key -out server.crt -days 36135
In the actual product, replace the certificate in main. c with your own certificate.
Running Result
Upon successful execution, it will output the following logs
flash device complete
connecting serial...
serial connected
[I] (10) main: ver: 2.3-alpha.2 build at Feb 6 2025 15:35:27
[I] (16) main: boot reason 4
[I] (19) main: heap size 262792(256.63KB)
[I] (23) main: flash size 0x200000(2MB)
W800: [I] (29) example_lws: Start Websocket server example
[I] (1972) wm_nm_wifi: start connecting new(12345678)
[I] (2717) wm_nm_core: new is connected
[I] (4463) wm_nm_core: sta got ip: 10.17.1.40
[I] (4477) example_lws: wifi station start success, network is ready!
[2025/02/06 16:06:30:4830] N: 179816: lws_create_context: LWS: 4.3.3-WM, MbedTLS-3.4.0 NET CLI SRV H1 WS ConMon IPv6-absent
[2025/02/06 16:06:30:5000] N: 179360: mem: platform fd map: 120 bytes
[2025/02/06 16:06:30:5070] N: 178584: __lws_lc_tag: ++ [wsi|0|pipe] (1)
[2025/02/06 16:06:30:5140] N: 177496: __lws_lc_tag: ++ [vh|0|default||443] (1)
[2025/02/06 16:06:30:5210] N: 177360: Vhost 'default' using TLS mode
[2025/02/06 16:06:30:5280] N: 177032: lws_tls_server_vhost_backend_init: vh default: mem CA OK
[2025/02/06 16:06:30:5410] N: 174744: no client cert required
[2025/02/06 16:06:30:5480] N: 173896: [vh|0|default||443]: lws_socket_bind: source ads 0.0.0.0
[2025/02/06 16:06:30:5570] N: 173264: __lws_lc_tag: ++ [wsi|1|listen|default||443] (2)
[I] (4565) example_lws: The websocket server listened on port 443
[2025/02/06 16:06:35:5470] N: 173264: [vh|0|default||443]: lws_tls_check_cert_lifetime: vhost default: cert expiry: -13577d
W800: [2025/02/06 16:06:52:6310] N: 171680: __lws_lc_tag: ++ [wsisrv|0|adopted] (1)
[2025/02/06 16:06:52:6390] N: 171680: lws_gate_accepts: on = 0
[2025/02/06 16:06:53:5540] N: 147248: lws_gate_accepts: on = 0
[I] (27609) example_lws: Client connected
[I] (31065) example_lws: Received msg from client:
hello
[I] (31072) example_lws: Send msg to client:
hello