WebSocket Client | How to get text data

mcmega
Posts: 16
Joined: Fri Oct 23, 2020 5:32 pm

WebSocket Client | How to get text data

Postby mcmega » 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.

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;
  }
}
In the parser function, I see that 23 bytes have arrived, but they are empty.
How to get data from WebSocket correctly?

chegewara
Posts: 2375
Joined: Wed Jun 14, 2017 9:00 pm

Re: WebSocket Client | How to get text data

Postby chegewara » Mon Jan 04, 2021 5:31 am


mcmega
Posts: 16
Joined: Fri Oct 23, 2020 5:32 pm

Re: WebSocket Client | How to get text data

Postby mcmega » Mon Jan 04, 2021 6:51 am

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

mcmega
Posts: 16
Joined: Fri Oct 23, 2020 5:32 pm

Re: WebSocket Client | How to get text data

Postby mcmega » Mon Jan 04, 2021 11:47 am

I figured it out, I had to remove the check.

Code: Select all

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);
}
And I installed this construction (checking for more than 8 because the shortest JSON object is 9 characters):

Code: Select all

if (data->data_len > 8) {
  isServerAvailable = SERVER_AVAILABLE;
  char * buffer;
  buffer = (char *) malloc(data->data_len * sizeof(char));
  if (buffer == NULL) {
    sendUART("ER!", "WS", "Unable to Allocate Buffer");
    break;
  }
  sprintf(buffer, "%.*s", data->data_len, (char *)data->data_ptr);
  parserWS(buffer, handler_args);
  free(buffer);
}
break;

Who is online

Users browsing this forum: No registered users and 358 guests