#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?