WebSocket Client | How to get text data
Posted: Mon Jan 04, 2021 5:26 am
Hello everyone! I am trying to use the Websocket Client example. I have no idea how to get a normal string.
I see that I can get a pointer to a string and its size. But I am not getting anything.
In the parser function, I see that 23 bytes have arrived, but they are empty.
How to get data from WebSocket correctly?
I see that I can get a pointer to a string and its size. But I am not getting anything.
Code: Select all
void parserWS(const char * data, int len) {
char * buffer;
buffer = (char *) malloc(len + 1);
if (buffer == NULL) {
sendUART("ER!", "WS", "Unable to Allocate Buffer");
return;
}
sprintf(buffer, "Len: %d, Data: %.s", len, data);
sendUART("OK!", "WS Received", buffer); // OK! WS Received Len: 23, Data:
free(buffer);
}
Code: Select all
static void websocketEventHandler(void * handler_args, esp_event_base_t base, int32_t event_id, void * event_data) {
esp_websocket_event_data_t * data = (esp_websocket_event_data_t *)event_data;
switch (event_id) {
case WEBSOCKET_EVENT_CONNECTED:
break;
case WEBSOCKET_EVENT_DISCONNECTED:
break;
case WEBSOCKET_EVENT_DATA:
if (data->op_code == 0x08 && data->data_len == 2) {
sendUART("OK!", "WS Received Closed Message", NULL);
} else if (data->data_len > 8) {
parserWS((char *)data->data_ptr, data->data_len);
}
break;
case WEBSOCKET_EVENT_ERROR:
break;
default:
break;
}
}
How to get data from WebSocket correctly?