Switch to binary websocket messages
This commit is contained in:
@@ -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) {
|
||||||
|
|||||||
37
main/main.c
37
main/main.c
@@ -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();
|
httpd_ws_frame_t ws_frame = {
|
||||||
cJSON *data_array = cJSON_CreateArray();
|
.final = true,
|
||||||
for (int i = 0; i < num_samples; i++) {
|
.fragmented = false,
|
||||||
cJSON_AddItemToArray(data_array, cJSON_CreateNumber(data[i]));
|
.type = HTTPD_WS_TYPE_BINARY,
|
||||||
}
|
.payload = (uint8_t *)data,
|
||||||
cJSON_AddItemToObject(root, "data", data_array);
|
.len = item_size
|
||||||
|
};
|
||||||
|
|
||||||
// Serialize JSON to string
|
// Send binary frame
|
||||||
char *json_str = cJSON_PrintUnformatted(root);
|
esp_err_t ret = httpd_ws_send_frame_async(s_server, s_ws_client_fd, &ws_frame);
|
||||||
if (json_str) {
|
if (ret != ESP_OK) {
|
||||||
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) {
|
|
||||||
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 {
|
||||||
|
|||||||
Reference in New Issue
Block a user