[Libwebsockets] How to pass a lws_callback_function from a c++ class

Pranjali Chumbhale pranjalic at safeai.ai
Wed Mar 18 01:24:38 CET 2020

Hello Team,

I am trying to pass  a c++ class member function as a callback to protocols.

Here is the relevant piece of code.

This is how my callback function is declared.
int ns::class_name::callback_ws( struct lws *wsi, enum lws_callback_reasons
void *user, void *in, size_t len);

This is how I am using it to pass it to protocols:
std::function<int(struct lws *wsi, enum lws_callback_reasons reason, void
*user, void *in, size_t len)> callback_func;

callback_func = std::bind(&ns::class_name::callback_ws,this,
std::placeholders::_1, std::placeholders::_2,
std::placeholders::_3, std::placeholders::_4,

m_protocols[0].name = "named_protocol";
m_protocols[0].callback = callback_func;
m_protocols[0].per_session_data_size = 0;
m_protocols[0].rx_buffer_size = m_max_data_size;

// terminator - needed by libwebsocket
m_protocols[1].name = NULL;
m_protocols[1].callback = NULL;
m_protocols[1].per_session_data_size = 0;
m_protocols[1].rx_buffer_size = 0;

I am getting the following error:

error: cannot convert ‘std::function<int(lws*, lws_callback_reasons, void*,
void*, long unsigned int)>’ to ‘int (*)(lws*, lws_callback_reasons, void*,
void*, size_t) {aka int (*)(lws*, lws_callback_reasons, void*, void*, long
unsigned int)}’ in assignment
   m_protocols[0].callback  = callback_func;
I do not understand when the return data type of callback function is int,
why is it throwing error looking for  int(*).

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://libwebsockets.org/pipermail/libwebsockets/attachments/20200317/b38def78/attachment-0001.htm>

More information about the Libwebsockets mailing list