[Libwebsockets] Accessing user data in websocket callback

Hemant Kumar hkumar at arubanetworks.com
Fri Aug 14 00:28:55 CEST 2015


Also as per my understanding is in callback function also the websocket passed in should have contained the user data set while calling connect.

callback_handler(struct libwebsocket_conext *ctx,
                                Struct libwebsocket *client_socket, <<<<This is same socket which we get after calling connect() if I am correct.
                                ..,...,..)

Thanks
Hemant

From: Hemant Kumar [mailto:andy.green at linaro.org]
Sent: Thursday, August 13, 2015 3:21 PM
To: libwebsockets at ml.libwebsockets.org
Subject: [Libwebsockets] Accessing user data in websocket callback


Hi

I am trying to set some user data so that to access it in libwebsocket callback and using libwebsocket_client_connect_extended() to achieve this.
But after connection gets ESTABLISHED, I do not see the same data.

Code construct:

Int a =5;

Main(..)
{

  client_socket = libwebsocket_client_connect_extended(context, address, port, use_ssl,
                            "/", address, address,
                            protocols[0].name, ietf_version, (void*)&a);<<<setting value as 5.



        if (client_socket == NULL) {
                fprintf(stderr, "libwebsocket connect failed\n");
                ret = 1;
                goto bail;
        }
        lwsl_notice("connected to server..%s user data %d\n",address, *((int*)(client_socket->user_space)));>>>correctly prints 5


}


callback(...)
{
 case LWS_CALLBACK_CLIENT_ESTABLISHED:

                    lwsl_notice("data_callback_handler: LWS_CALLBACK_CLIENT_ESTABLISHED\n");

                    lwsl_notice("user data %d\n",*((int*)(client_socket->user_space)));<<<<<<<<<<<<Incorrect prints 0
                    libwebsocket_callback_on_writable(context, client_socket);


case LWS_CALLBACK_CLIENT_WRITEABLE:
                    lwsl_notice("LWS_CALLBACK_CLIENT_WRITEABLE\n");
                    lwsl_notice("user data %d\n",*((int*)(client_socket->user_space)));<<<<<<<<<<<Incorrect prints 0

}
Can someone please suggest what I am doing wrong here?

Any help will be appreciated.

Thanks
Hemant

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://libwebsockets.org/pipermail/libwebsockets/attachments/20150813/c8bbe391/attachment-0001.html>


More information about the Libwebsockets mailing list