0

This is my firts time posting something so please be kind and correct me, if i did something wrong. Here is my problem: I currently work with an esp8266 and use the ESPAsyncWebServer library. I would like my code to be object oriented. In the internet I found this piece of code:

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;
}

}

corresponding with this in my WifiConnector class:

void  Wifi::WifiConnector::initWebSocket()
{
ws.onEvent(onEvent);
server.addHandler(&ws);
}

The only problem is, that if I try to change the global onEvent function to a public method (in my WifiConnector class) the code will just not work. I figured out that the ws.onEvent method looks like this in the library:

void onEvent(AwsEventHandler handler){
  _eventHandler = handler;
}

and the AwsEventHandler is this:

typedef std::function<void(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len)> AwsEventHandler;

I dont really get how these callbacks work so it would be great if you could help me out. Thanks in advance.

Fabian
  • 1

0 Answers0