From f77e4ae39a7a14be49bd6880c94933812883a77c Mon Sep 17 00:00:00 2001 From: Matt Date: Fri, 12 Dec 2025 23:14:49 +0000 Subject: [PATCH] Switch to binary websocket messages --- main/index.js | 8 ++++---- main/main.c | 37 +++++++++++-------------------------- 2 files changed, 15 insertions(+), 30 deletions(-) diff --git a/main/index.js b/main/index.js index 63a5141..41a5014 100644 --- a/main/index.js +++ b/main/index.js @@ -177,7 +177,7 @@ function connect() { // const wsUrl = 'ws://localhost:8080/signal'; ws = new WebSocket(wsUrl); - + ws.binaryType = 'arraybuffer'; ws.onopen = () => { statusEl.textContent = 'Connected via WebSocket'; statusEl.style.color = '#4ade80'; @@ -190,9 +190,9 @@ function connect() { ws.onmessage = (event) => { try { - const msg = JSON.parse(event.data); - if (msg.data && Array.isArray(msg.data)) { - processData(msg.data); + const arr = new Uint16Array(event.data); + if (arr?.length) { + processData(arr); draw(); } } catch (e) { diff --git a/main/main.c b/main/main.c index a7e7bc5..66d6e0d 100644 --- a/main/main.c +++ b/main/main.c @@ -482,37 +482,22 @@ static void ws_sender_task(void *arg) { s_ringbuf_handle, &item_size, pdMS_TO_TICKS(10)); if (data != NULL) { - int num_samples = item_size / sizeof(uint16_t); if (s_ws_client_fd != -1) { - // Create JSON object - cJSON *root = cJSON_CreateObject(); - cJSON *data_array = cJSON_CreateArray(); - for (int i = 0; i < num_samples; i++) { - cJSON_AddItemToArray(data_array, cJSON_CreateNumber(data[i])); - } - cJSON_AddItemToObject(root, "data", data_array); + // Create WebSocket frame for binary data + httpd_ws_frame_t ws_frame = { + .final = true, + .fragmented = false, + .type = HTTPD_WS_TYPE_BINARY, + .payload = (uint8_t *)data, + .len = item_size + }; - // Serialize JSON to string - char *json_str = cJSON_PrintUnformatted(root); - if (json_str) { - httpd_ws_frame_t ws_frame = { - .final = true, - .fragmented = false, - .type = HTTPD_WS_TYPE_TEXT, - .payload = (uint8_t *)json_str, - .len = strlen(json_str)}; - - // Send JSON frame - esp_err_t ret = httpd_ws_send_frame_async(s_server, s_ws_client_fd, &ws_frame); - if (ret != ESP_OK) { + // Send binary frame + esp_err_t ret = httpd_ws_send_frame_async(s_server, s_ws_client_fd, &ws_frame); + if (ret != ESP_OK) { ESP_LOGW(TAG, "WS Send failed, invalidating FD"); s_ws_client_fd = -1; - } - - free(json_str); } - - cJSON_Delete(root); } vRingbufferReturnItem(s_ringbuf_handle, (void *)data); } else {