如何在 libwebsockets 回调中设置用户自定义指针?
我在 lws_protocol 中添加了一个指针变量。
调用回调函数时,用户指针始终为 NULL。
我使用 libwebsockets v3.0。
static int interrupted, rx_seen, test;
int ws_callback(struct lws *ws, enum lws_callback_reasons reason, void *user, void *in, size_t len) {
// user is NULL
return lws_callback_http_dummy(ws, reason, user, in, len);
}
int main() {
struct lws *ws;
struct lws_context_creation_info context_info;
struct lws_client_connect_info client_info;
struct lws_context *context;
struct lws_protocols protocols[] = {
{ "ws_callback", ws_callback, 0, 0, 0, POINTER_VARIABLE /* HERE */, 0 }
};
int n = 0;
// context creation info
memset(&context_info, 0, sizeof(context_info));
context_info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
context_info.port = CONTEXT_PORT_NO_LISTEN;
context_info.protocols = protocols;
context = lws_create_context(&context_info);
if (!context) {
return;
}
memset(&client_info, 0, sizeof(client_info));
client_info.context = context;
client_info.port = 8080;
client_info.address = "192.168.1.1";
client_info.path = "/";
client_info.host = client_info.address;
client_info.origin = client_info.address;
client_info.protocol = protocols[0].name;
client_info.pwsi = &ws;
lws_client_connect_via_info(&client_info);
while (n >= 0 && ws && !interrupted) {
n = lws_service(context, 1000);
}
lws_context_destroy(context);
return 0;
}