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';
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) {

View File

@@ -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);
// Serialize JSON to string
char *json_str = cJSON_PrintUnformatted(root);
if (json_str) {
// Create WebSocket frame for binary data
httpd_ws_frame_t ws_frame = {
.final = true,
.fragmented = false,
.type = HTTPD_WS_TYPE_TEXT,
.payload = (uint8_t *)json_str,
.len = strlen(json_str)};
.type = HTTPD_WS_TYPE_BINARY,
.payload = (uint8_t *)data,
.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);
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 {