Attaching callback function to derived WebSocketsServer class error

Hello, I have been trying to do this the whole day but it just doesn’t work the way I want it to. I am trying to write my own library for the ESP8266 for things I use in all my projects. One thing is a slightly modified myWebSockets class which I derive from the WebSocketsServer class. My code is highly simplified so there is a lot of stuff missing so I can post it here without you guys having to look through many lines of code:

#include <WebSocketsServer.h>

class myWebSockets : WebSocketsServer
{
  public:
  myWebSockets(uint16_t port, String origin = "", String protocol = "arduino") : WebSocketsServer(port, origin, protocol)
  {
    onEvent(callback);
  }
  void callback(uint8_t num, WStype_t type, uint8_t *payload, size_t lenght)
  {
    
  }
};

myWebSockets ws(81);

void setup() {}

void loop() {}

Now I get the following error:

<SOMEPATH>\sketch_apr19a\sketch_apr19a.ino: In constructor 'myWebSockets::myWebSockets(uint16_t, String, String)':

sketch_apr19a:8: error: no matching function for call to 'myWebSockets::onEvent(<unresolved overloaded function type>)'

     onEvent(callback);

                     ^

<SOMEPATH>\sketch_apr19a\sketch_apr19a.ino:8:21: note: candidate is:

In file included from <SOMEPATH>\sketch_apr19a\sketch_apr19a.ino:1:0:

<SOMEPATH>\Arduino\libraries\WebSockets\src/WebSocketsServer.h:57:14: note: void WebSocketsServer::onEvent(WebSocketsServer::WebSocketServerEvent)

         void onEvent(WebSocketServerEvent cbEvent);

              ^

<SOMEPATH>\Arduino\libraries\WebSockets\src/WebSocketsServer.h:57:14: note:   no known conversion for argument 1 from '<unresolved overloaded function type>' to 'WebSocketsServer::WebSocketServerEvent {aka std::function<void(unsigned char, WStype_t, unsigned char*, unsigned int)>}'

I got it to work by not deriving the myWebSockets class from WebSocketsServer and calling onEvent(callback) on an object of type WebSocketsServer, so the callback type should be fine, but it doesn’t work in this case for whatever reason. I really want to derive my own class though.

Does anybody see my mistake here? Thanks a lot for reading!

Edit:
I forgot to mention: When I change my code to

#include <WebSocketsServer.h>

class myWebSockets : WebSocketsServer
{
  public:
  myWebSockets(uint16_t port, String origin = "", String protocol = "arduino") : WebSocketsServer(port, origin, protocol)
  {
    onEvent(callback);
  }
  WebSocketServerEvent callback;
};

myWebSockets ws(81, "", "");

void setup() {}

void loop() {}

it compiles fine but then how can I define the callback function? If I do it with myWebSockets::WebSocketServerEvent myWebSockets::callback() outside of the class what goes into the brackets?

The onEvent() method takes an event, NOT the name of the function to call. It is silly to name the variable callback when it is an event. Don't even think of doing that.

Post a link to the WebSocketServer library.

So I should rather do it like in my second code example? But how do I assign the function of the event? The library can be found here: GitHub - Links2004/arduinoWebSockets: arduinoWebSockets

My code is also based on the example of said library here: https://github.com/Links2004/arduinoWebSockets/blob/master/examples/WebSocketServer_LEDcontrol/WebSocketServer_LEDcontrol.ino It works fine right there but I don't understand why it doesn't work when I put all this in a new class that derives from WebSocketsServer.

So I should rather do it like in my second code example?

No. That was the one that I found abominable.

The onEvent() method takes an event. It is silly to name the event callback. The object is NOT a callback. It is the event that triggers the callback.

Thanks, but I still don't understand what's wrong with my code considering the example that comes with the library. The only thing I understood so far is that I mistakenly renamed my event to "callback" instead of leaving it as "webSocketEvent".

Thanks, but I still don't understand what's wrong with my code

Which code? The first code is wrong because you are defining a function called callback and passing the function (address) to a function that expects something completely different.

The second code is marginally better, since the type of the object called callback is now the correct type (though the name leaves a lot to be desired).

Which code do you want to go forward with?

I am very confused because you just called my second code example “abominable” but it is still better than the first one? I don’t know either which code i want to go further with. But let’s now take this code:

#include <WebSocketsServer.h>

class myWebSockets : WebSocketsServer
{
  public:
  myWebSockets(uint16_t port, String origin = "", String protocol = "arduino") : WebSocketsServer(port, origin, protocol)
  {
    onEvent(webSocketEvent);
  }
  WebSocketServerEvent webSocketEvent;
};

myWebSockets ws(81);

void setup() {}

void loop() {}

So this is correct so far? Now I need to assign the actual function to my event. So when it gets triggered it has to actually DO something. My question is how I can define what webSocketEvent actually does. Please don’t be too pedantic as I really am out of ideas and have been trying to get it to work for many hours now. I could really learn a lot from a simple code example (based on what I posted above) to learn what my code has to look like.

I am very confused because you just called my second code example "abominable" but it is still better than the first one?

Suppose I wrote a function:

void takeFloat(float a);

Now, suppose that I defined a variable:

int myFloat = 3.14159;

What would you expect takeFloat() to do, when I pass it myFloat?

Just because I called the variable myFloat doesn't make it a float.

In fact, calling it myFloat looks pretty silly, doesn't it?

Well, that is what you are doing, and why the code is abominable.

On the other hand, at least the type of the variable is compatible with the function that it is being passed to.

So this is correct so far?

As far as I can tell, yes.

Now I need to assign the actual function to my event.

How is that handled? Typically, the onEvent() function would call one function, registered somewhere else, when an event occurs, regardless of that kind of event happens.

My question is how I can define what webSocketEvent actually does.

You don't. An event is something that happens, like the phone ringing, the kid crying, the car alarm going off, etc. You don't control the events. You control what happens as a result of the event happening.

Changing the kid's diaper because the car alarm went off would be pretty silly.

Running out to see who's messing with the car because the phone rang would be silly, too.

The callback ("Hey, buddy, you need to do something because event Xxxx happened") needs to take appropriate action based on the event.

Where is the callback function (that onEvent() is to call) registered?

I'm not sure why you are trying to extend a class that you don't seem to understand. What is your extension suppose to do better/different?

An event is something that happens, like the phone ringing, the kid crying, the car alarm going off, etc. You don’t control the events. You control what happens as a result of the event happening.

Well, I am confusing the terminology a bit here. Looks like I don’t want to change the event itself but what happens as a result of the event being triggered.

Where is the callback function (that onEvent() is to call) registered?

So that is what you call a callback function, right? Because that is what I wanted to code from the beginning so calling it “callback” wasn’t so wrong? But I really don’t want to argue about the terminology here. I just want my code to run.

In the meantime I came up with this and it compiles fine but I am not 100% sure if it does what I expect it to do:

#include <WebSocketsServer.h>

class myWebSockets : WebSocketsServer
{
  public:
  myWebSockets(uint16_t port, String origin = "", String protocol = "arduino") : WebSocketsServer(port, origin, protocol)
  {
    webSocketEvent = [&](unsigned char, WStype_t, unsigned char*, unsigned int){ callback(); };
    onEvent(webSocketEvent);
  }
  void callback()
  {
    // do something as a result of the event being triggered...
  }
  WebSocketServerEvent webSocketEvent;
};

myWebSockets ws(81);

void setup() {}

void loop() {}

When the event gets fired it should call the callback function and do appropriate stuff…

I’m not sure why you are trying to extend a class that you don’t seem to understand. What is your extension suppose to do better/different?

In my projects I noticed I am always doing the same things and want to integrate them into my own library so I can enhance them in the future without having to change the code in all my projects.