I read it.
Now another problem arose.
I send 3-5 requests to the server and then get 3-5 responses at once.
I need to receive responses for every request.
My complete code:
Code: Select all
esp_err_t webSocketInit() {
if (xTaskCreate(taskWebSocket, "taskWebSocket", 4096, NULL, 6, &xHandleWebSocket) != pdPASS) {
sendUART("ER!", "taskWebSocket", NULL);
return ESP_FAIL;
}
vTaskSuspend(xHandleWebSocket);
return ESP_OK;
}
Code: Select all
static void taskWebSocket() {
while (1) {
esp_websocket_client_config_t websocket_cfg = {};
websocket_cfg.uri = WEB_SOCKET_URI;
websocket_cfg.port = WEB_SOCKET_PORT;
esp_websocket_client_handle_t client = esp_websocket_client_init(&websocket_cfg);
esp_websocket_register_events(client, WEBSOCKET_EVENT_ANY, websocketEventHandler, (void *)client);
esp_websocket_client_start(client);
int count = 0;
while (1) {
if (esp_websocket_client_is_connected(client) == true) {
cJSON * root = cJSON_CreateObject();
cJSON_AddStringToObject(root, "DevSN", getDeviceSN(commonBuffer));
char * jsonString = cJSON_PrintUnformatted(root);
int len = strlen(jsonString);
esp_websocket_client_send_text(client, jsonString, len, portMAX_DELAY); //{"DevSN":"FF008-6D1FC8C-F2DC0"}
sendUART("OK!", "WS Sended", jsonString);
cJSON_free(jsonString);
cJSON_Delete(root);
} else if (count > COUNT_RESTART_WS) {
break;
} else {
count++;
}
vTaskDelay(PERIOD_CHECK_SERVER * 1000 / portTICK_PERIOD_MS);
}
esp_websocket_client_stop(client);
esp_websocket_client_destroy(client);
}
vTaskDelete(NULL);
}
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 {
parserWS(data->data_len, (char *)data->data_ptr);
}
break;
case WEBSOCKET_EVENT_ERROR:
break;
default:
break;
}
}
Code: Select all
void parserWS(int len, const char * data) {
printf("%.*s", len, data);
char * buffer;
buffer = (char *) malloc(len * sizeof(char));
if (buffer == NULL) {
sendUART("ER!", "WS", "Unable to Allocate Buffer");
return;
}
sprintf(buffer, "%.*s", len, data);
free(buffer);
}
Also I am unable to allocate memory for the received packet (malloc).
Example of output to the terminal (189412 - Free heap size):
Code: Select all
OK! WS Sended {"DevSN":"FF008-6D1FC8C-F2DC0"} 7CB5 (189608)
ER! WS Unable to Allocate Buffer 3837 (193024)
OK! WS Sended {"DevSN":"FF008-6D1FC8C-F2DC0"} 7CB5 (189608)
OK! WS Sended {"DevSN":"FF008-6D1FC8C-F2DC0"} 7CB5 (189608)
ER! WS Unable to Allocate Buffer 3837 (193024)
OK! WS Sended {"DevSN":"FF008-6D1FC8C-F2DC0"} 7CB5 (189608)
OK! WS Sended {"DevSN":"FF008-6D1FC8C-F2DC0"} 7CB5 (189808)
Hi there, I am a WebSocket server{"error":"Wrong query"}{"error":"Wrong query"}{"error":"Wrong query"}{"error":"Wrong query"}{"e
OK! WS Sended {"DevSN":"FF008-6D1FC8C-F2DC0"} 7CB5 (189524)
ER! WS Unable to Allocate Buffer 3837 (192940)
OK! WS Sended {"DevSN":"FF008-6D1FC8C-F2DC0"} 7CB5 (189524)
OK! WS Sended {"DevSN":"FF008-6D1FC8C-F2DC0"} 7CB5 (189524)
ER! WS Unable to Allocate Buffer 3837 (192940)
OK! WS Sended {"DevSN":"FF008-6D1FC8C-F2DC0"} 7CB5 (189524)
OK! WS Sended {"DevSN":"FF008-6D1FC8C-F2DC0"} 7CB5 (189524)
rror":"Wrong query"}{"error":"Wrong query"}{"error":"Wrong query"}{"error":"Wrong query"}{"error":"Wrong query"}{"error":"Wrong