• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

understanding handleWebSocketMessage

Q&A Forum › Category: Other › understanding handleWebSocketMessage
0 Vote Up Vote Down
Wally Harwood asked 7 months ago

I am having trouble understanding the callback function which occurs in many of the examples in Build Web Servers eBook – 2nd Edition  (eg. pg. 287 or  pg. 337)
void handleWebSocketMessage(void *arg, uint8_t *data, size_t len) {
AwsFrameInfo *info = (AwsFrameInfo*)arg;
if (info->final && info->index == 0 && info->len == len && info->opcode == WS_TEXT) {
data[len] = 0;
if (strcmp((char*)data, “states”) == 0) {
notifyClients(getOutputStates());
}
else{
int gpio = atoi((char*)data);
digitalWrite(gpio, !digitalRead(gpio));
notifyClients(getOutputStates());
}
I would like to know how the code works in much more detail than in the How the Code Works section.

Question Tags: Websocket message handler
5 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 7 months ago

Hi.
The handleWebSocketMessage() message will be called when a new websocket event data happens. See the onEvent function:

void onEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type,
             void *arg, uint8_t *data, size_t len) {
  switch (type) {
    case WS_EVT_CONNECT:
      Serial.printf("WebSocket client #%u connected from %s\n", client->id(), client->remoteIP().toString().c_str());
      break;
    case WS_EVT_DISCONNECT:
      Serial.printf("WebSocket client #%u disconnected\n", client->id());
      break;
    case WS_EVT_DATA:
      handleWebSocketMessage(arg, data, len);
      break;
    case WS_EVT_PONG:
    case WS_EVT_ERROR:
      break;
  }
}

arg, data, and len are parameters passed automatically to the handleWebSocketMessage() callback function.

Inside the handleWebSocketMessage, arg (which is a WebSocket frame) is cast to the AwsFrameInfo type. This structure contains metadata such as whether the message is complete, its length, and its type (binary or text).
The following line:

if (info->final && info->index == 0 && info->len == len && info->opcode == WS_TEXT) {

checks if the WebSocket message is complete, has the expected length, and is a text message. If so, we have a valid websocket message.

Then, we basically check the content of the received data , which is saved on the data variable, and act accordingly. The strcmp() function compares two strings character by character and  returns 0 if the two strings are identical.

I hope this helps.

Regards,
Sara

0 Vote Up Vote Down
Wally Harwood answered 7 months ago

Thank you very much Sara,
This is exactly what i needed to know.
Am I correct in understanding that “arg, data, and len ” and their memory locations originate <AsyncTCP.h> or  <ESPAsyncWebServer.h> libraries? Hence your use of “automatically”
Thank you again for a very comprehensive and comprehensible answer
Wally
 

0 Vote Up Vote Down
Sara Santos Staff answered 7 months ago

Hi.
Yes.
That’s correct. It’s how the library works.
 
Regards,
Sara

0 Vote Up Vote Down
Wally Harwood answered 7 months ago

Sara,
Thank you again.  You are a super teacher. All the best. 
Wally

0 Vote Up Vote Down
Sara Santos Staff answered 7 months ago

Thanks.
I’ll mark this issue as resolved. If you need further help, you just need to open a new question in our forum.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.