-->
Page 1 of 1

Attaching callback function to derived WebSocketsServer clas

PostPosted: Tue Apr 19, 2016 6:53 pm
by frischevollmilch
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:
Code: Select all#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:

Code: Select all<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
Code: Select all#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?