Switch to binary websocket messages

This commit is contained in:
Matt
2025-12-12 23:14:49 +00:00
parent 5bc3940129
commit f77e4ae39a
2 changed files with 15 additions and 30 deletions

View File

@@ -177,7 +177,7 @@ function connect() {
// const wsUrl = 'ws://localhost:8080/signal'; // const wsUrl = 'ws://localhost:8080/signal';
ws = new WebSocket(wsUrl); ws = new WebSocket(wsUrl);
ws.binaryType = 'arraybuffer';
ws.onopen = () => { ws.onopen = () => {
statusEl.textContent = 'Connected via WebSocket'; statusEl.textContent = 'Connected via WebSocket';
statusEl.style.color = '#4ade80'; statusEl.style.color = '#4ade80';
@@ -190,9 +190,9 @@ function connect() {
ws.onmessage = (event) => { ws.onmessage = (event) => {
try { try {
const msg = JSON.parse(event.data); const arr = new Uint16Array(event.data);
if (msg.data && Array.isArray(msg.data)) { if (arr?.length) {
processData(msg.data); processData(arr);
draw(); draw();
} }
} catch (e) { } catch (e) {

View File

@@ -482,37 +482,22 @@ static void ws_sender_task(void *arg) {
s_ringbuf_handle, &item_size, pdMS_TO_TICKS(10)); s_ringbuf_handle, &item_size, pdMS_TO_TICKS(10));
if (data != NULL) { if (data != NULL) {
int num_samples = item_size / sizeof(uint16_t);
if (s_ws_client_fd != -1) { if (s_ws_client_fd != -1) {
// Create JSON object // Create WebSocket frame for binary data
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);
// Serialize JSON to string
char *json_str = cJSON_PrintUnformatted(root);
if (json_str) {
httpd_ws_frame_t ws_frame = { httpd_ws_frame_t ws_frame = {
.final = true, .final = true,
.fragmented = false, .fragmented = false,
.type = HTTPD_WS_TYPE_TEXT, .type = HTTPD_WS_TYPE_BINARY,
.payload = (uint8_t *)json_str, .payload = (uint8_t *)data,
.len = strlen(json_str)}; .len = item_size
};
// Send JSON frame // Send binary frame
esp_err_t ret = httpd_ws_send_frame_async(s_server, s_ws_client_fd, &ws_frame); esp_err_t ret = httpd_ws_send_frame_async(s_server, s_ws_client_fd, &ws_frame);
if (ret != ESP_OK) { if (ret != ESP_OK) {
ESP_LOGW(TAG, "WS Send failed, invalidating FD"); ESP_LOGW(TAG, "WS Send failed, invalidating FD");
s_ws_client_fd = -1; s_ws_client_fd = -1;
} }
free(json_str);
}
cJSON_Delete(root);
} }
vRingbufferReturnItem(s_ringbuf_handle, (void *)data); vRingbufferReturnItem(s_ringbuf_handle, (void *)data);
} else { } else {