Select one of the symbols to view example projects that use it.
 
Outline
#include <esp_event.h>
#include <esp_log.h>
#include <esp_system.h>
#include <nvs_flash.h>
#include <sys/param.h>
#include "esp_netif.h"
#include "esp_eth.h"
#include "esp_wifi.h"
#include "protocol_examples_common.h"
#include "lwip/sockets.h"
#include <esp_https_server.h>
#include "keep_alive.h"
#include "sdkconfig.h"
async_resp_arg
TAG
max_clients
ws_handler(httpd_req_t *)
wss_open_fd(httpd_handle_t, int)
wss_close_fd(httpd_handle_t, int)
ws
send_hello(void *)
send_ping(void *)
client_not_alive_cb(wss_keep_alive_t, int)
check_client_alive_cb(wss_keep_alive_t, int)
start_wss_echo_server()
stop_wss_echo_server(httpd_handle_t)
disconnect_handler(void *, esp_event_base_t, int32_t, void *)
connect_handler(void *, esp_event_base_t, int32_t, void *)
wss_server_send_messages(httpd_handle_t *)
app_main()
Files
loading...
SourceVuESP-IDF Framework and Exampleswss_server samplemain/wss_server_example.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* Simple HTTP + SSL + WS Server Example This example code is in the Public Domain (or CC0 licensed, at your option.) Unless required by applicable law or agreed to in writing, this software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *//* ... */ #include <esp_event.h> #include <esp_log.h> #include <esp_system.h> #include <nvs_flash.h> #include <sys/param.h> #include "esp_netif.h" #include "esp_eth.h" #include "esp_wifi.h" #include "protocol_examples_common.h" #include "lwip/sockets.h" #include <esp_https_server.h> #include "keep_alive.h" #include "sdkconfig.h"13 includes #if !CONFIG_HTTPD_WS_SUPPORT #error This example cannot be used unless HTTPD_WS_SUPPORT is enabled in esp-http-server component configuration #endif struct async_resp_arg { httpd_handle_t hd; int fd; }{ ... }; static const char *TAG = "wss_echo_server"; static const size_t max_clients = 4; static esp_err_t ws_handler(httpd_req_t *req) { if (req->method == HTTP_GET) { ESP_LOGI(TAG, "Handshake done, the new connection was opened"); return ESP_OK; }{...} httpd_ws_frame_t ws_pkt; uint8_t *buf = NULL; memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t)); // First receive the full ws message /* Set max_len = 0 to get the frame len */ esp_err_t ret = httpd_ws_recv_frame(req, &ws_pkt, 0); if (ret != ESP_OK) { ESP_LOGE(TAG, "httpd_ws_recv_frame failed to get frame len with %d", ret); return ret; }{...} ESP_LOGI(TAG, "frame len is %d", ws_pkt.len); if (ws_pkt.len) { /* ws_pkt.len + 1 is for NULL termination as we are expecting a string */ buf = calloc(1, ws_pkt.len + 1); if (buf == NULL) { ESP_LOGE(TAG, "Failed to calloc memory for buf"); return ESP_ERR_NO_MEM; }{...} ws_pkt.payload = buf; /* Set max_len = ws_pkt.len to get the frame payload */ ret = httpd_ws_recv_frame(req, &ws_pkt, ws_pkt.len); if (ret != ESP_OK) { ESP_LOGE(TAG, "httpd_ws_recv_frame failed with %d", ret); free(buf); return ret; }{...} }{...} // If it was a PONG, update the keep-alive if (ws_pkt.type == HTTPD_WS_TYPE_PONG) { ESP_LOGD(TAG, "Received PONG message"); free(buf); return wss_keep_alive_client_is_active(httpd_get_global_user_ctx(req->handle), httpd_req_to_sockfd(req)); // If it was a TEXT message, just echo it back }{...} else if (ws_pkt.type == HTTPD_WS_TYPE_TEXT || ws_pkt.type == HTTPD_WS_TYPE_PING || ws_pkt.type == HTTPD_WS_TYPE_CLOSE) { if (ws_pkt.type == HTTPD_WS_TYPE_TEXT) { ESP_LOGI(TAG, "Received packet with message: %s", ws_pkt.payload); }{...} else if (ws_pkt.type == HTTPD_WS_TYPE_PING) { // Response PONG packet to peer ESP_LOGI(TAG, "Got a WS PING frame, Replying PONG"); ws_pkt.type = HTTPD_WS_TYPE_PONG; }{...} else if (ws_pkt.type == HTTPD_WS_TYPE_CLOSE) { // Response CLOSE packet with no payload to peer ws_pkt.len = 0; ws_pkt.payload = NULL; }{...} ret = httpd_ws_send_frame(req, &ws_pkt); if (ret != ESP_OK) { ESP_LOGE(TAG, "httpd_ws_send_frame failed with %d", ret); }{...} ESP_LOGI(TAG, "ws_handler: httpd_handle_t=%p, sockfd=%d, client_info:%d", req->handle, httpd_req_to_sockfd(req), httpd_ws_get_fd_info(req->handle, httpd_req_to_sockfd(req))); free(buf); return ret; }{...} free(buf); return ESP_OK; }{ ... } esp_err_t wss_open_fd(httpd_handle_t hd, int sockfd) { ESP_LOGI(TAG, "New client connected %d", sockfd); wss_keep_alive_t h = httpd_get_global_user_ctx(hd); return wss_keep_alive_add_client(h, sockfd); }{ ... } void wss_close_fd(httpd_handle_t hd, int sockfd) { ESP_LOGI(TAG, "Client disconnected %d", sockfd); wss_keep_alive_t h = httpd_get_global_user_ctx(hd); wss_keep_alive_remove_client(h, sockfd); close(sockfd); }{ ... } static const httpd_uri_t ws = { .uri = "/ws", .method = HTTP_GET, .handler = ws_handler, .user_ctx = NULL, .is_websocket = true, .handle_ws_control_frames = true }{...}; static void send_hello(void *arg) { static const char * data = "Hello client"; struct async_resp_arg *resp_arg = arg; httpd_handle_t hd = resp_arg->hd; int fd = resp_arg->fd; httpd_ws_frame_t ws_pkt; memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t)); ws_pkt.payload = (uint8_t*)data; ws_pkt.len = strlen(data); ws_pkt.type = HTTPD_WS_TYPE_TEXT; httpd_ws_send_frame_async(hd, fd, &ws_pkt); free(resp_arg); }{ ... } static void send_ping(void *arg) { struct async_resp_arg *resp_arg = arg; httpd_handle_t hd = resp_arg->hd; int fd = resp_arg->fd; httpd_ws_frame_t ws_pkt; memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t)); ws_pkt.payload = NULL; ws_pkt.len = 0; ws_pkt.type = HTTPD_WS_TYPE_PING; httpd_ws_send_frame_async(hd, fd, &ws_pkt); free(resp_arg); }{ ... } bool client_not_alive_cb(wss_keep_alive_t h, int fd) { ESP_LOGE(TAG, "Client not alive, closing fd %d", fd); httpd_sess_trigger_close(wss_keep_alive_get_user_ctx(h), fd); return true; }{ ... } bool check_client_alive_cb(wss_keep_alive_t h, int fd) { ESP_LOGD(TAG, "Checking if client (fd=%d) is alive", fd); struct async_resp_arg *resp_arg = malloc(sizeof(struct async_resp_arg)); assert(resp_arg != NULL); resp_arg->hd = wss_keep_alive_get_user_ctx(h); resp_arg->fd = fd; if (httpd_queue_work(resp_arg->hd, send_ping, resp_arg) == ESP_OK) { return true; }{...} return false; }{ ... } static httpd_handle_t start_wss_echo_server(void) { // Prepare keep-alive engine wss_keep_alive_config_t keep_alive_config = KEEP_ALIVE_CONFIG_DEFAULT(); keep_alive_config.max_clients = max_clients; keep_alive_config.client_not_alive_cb = client_not_alive_cb; keep_alive_config.check_client_alive_cb = check_client_alive_cb; wss_keep_alive_t keep_alive = wss_keep_alive_start(&keep_alive_config); // Start the httpd server httpd_handle_t server = NULL; ESP_LOGI(TAG, "Starting server"); httpd_ssl_config_t conf = HTTPD_SSL_CONFIG_DEFAULT(); conf.httpd.max_open_sockets = max_clients; conf.httpd.global_user_ctx = keep_alive; conf.httpd.open_fn = wss_open_fd; conf.httpd.close_fn = wss_close_fd; extern const unsigned char servercert_start[] asm("_binary_servercert_pem_start"); extern const unsigned char servercert_end[] asm("_binary_servercert_pem_end"); conf.servercert = servercert_start; conf.servercert_len = servercert_end - servercert_start; extern const unsigned char prvtkey_pem_start[] asm("_binary_prvtkey_pem_start"); extern const unsigned char prvtkey_pem_end[] asm("_binary_prvtkey_pem_end"); conf.prvtkey_pem = prvtkey_pem_start; conf.prvtkey_len = prvtkey_pem_end - prvtkey_pem_start; esp_err_t ret = httpd_ssl_start(&server, &conf); if (ESP_OK != ret) { ESP_LOGI(TAG, "Error starting server!"); return NULL; }{...} // Set URI handlers ESP_LOGI(TAG, "Registering URI handlers"); httpd_register_uri_handler(server, &ws); wss_keep_alive_set_user_ctx(keep_alive, server); return server; }{ ... } static esp_err_t stop_wss_echo_server(httpd_handle_t server) { // Stop keep alive thread wss_keep_alive_stop(httpd_get_global_user_ctx(server)); // Stop the httpd server return httpd_ssl_stop(server); }{ ... } static void disconnect_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { httpd_handle_t* server = (httpd_handle_t*) arg; if (*server) { if (stop_wss_echo_server(*server) == ESP_OK) { *server = NULL; }{...} else { ESP_LOGE(TAG, "Failed to stop https server"); }{...} }{...} }{ ... } static void connect_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { httpd_handle_t* server = (httpd_handle_t*) arg; if (*server == NULL) { *server = start_wss_echo_server(); }{...} }{ ... } // Get all clients and send async message static void wss_server_send_messages(httpd_handle_t* server) { bool send_messages = true; // Send async message to all connected clients that use websocket protocol every 10 seconds while (send_messages) { vTaskDelay(10000 / portTICK_PERIOD_MS); if (!*server) { // httpd might not have been created by now continue; }{...} size_t clients = max_clients; int client_fds[max_clients]; if (httpd_get_client_list(*server, &clients, client_fds) == ESP_OK) { for (size_t i=0; i < clients; ++i) { int sock = client_fds[i]; if (httpd_ws_get_fd_info(*server, sock) == HTTPD_WS_CLIENT_WEBSOCKET) { ESP_LOGI(TAG, "Active client (fd=%d) -> sending async message", sock); struct async_resp_arg *resp_arg = malloc(sizeof(struct async_resp_arg)); assert(resp_arg != NULL); resp_arg->hd = *server; resp_arg->fd = sock; if (httpd_queue_work(resp_arg->hd, send_hello, resp_arg) != ESP_OK) { ESP_LOGE(TAG, "httpd_queue_work failed!"); send_messages = false; break; }{...} }{...} }{...} }{...} else { ESP_LOGE(TAG, "httpd_get_client_list failed!"); return; }{...} }{...} }{ ... } void app_main(void) { static httpd_handle_t server = NULL; ESP_ERROR_CHECK(nvs_flash_init()); ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* Register event handlers to start server when Wi-Fi or Ethernet is connected, * and stop server when disconnection happens. *//* ... */ #ifdef CONFIG_EXAMPLE_CONNECT_WIFI ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, &server)); ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server));/* ... */ #endif // CONFIG_EXAMPLE_CONNECT_WIFI #ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &connect_handler, &server)); ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_DISCONNECTED, &disconnect_handler, &server));/* ... */ #endif // CONFIG_EXAMPLE_CONNECT_ETHERNET /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. * Read "Establishing Wi-Fi or Ethernet Connection" section in * examples/protocols/README.md for more information about this function. *//* ... */ ESP_ERROR_CHECK(example_connect()); /* This function demonstrates periodic sending Websocket messages * to all connected clients to this server *//* ... */ wss_server_send_messages(&server); }{ ... }
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.