[Libwebsockets] Accessing user data in websocket callback

Andy Green andy at warmcat.com
Thu Oct 15 04:32:53 CEST 2015



On 14 August 2015 08:41:26 GMT+09:00, Hemant Kumar <hkumar at arubanetworks.com> wrote:
>Hi
>
>Okay, I think I understand this. The user_space in wsi is meant for
>per-connection user data
>and is initialized everytime in following function:
>
>int
>libwebsocket_ensure_user_space(struct libwebsocket *wsi)
>{
>    if (!wsi->protocol)
>        return 1;
>
>    /* allocate the per-connection user memory (if any) */
>
>    if (wsi->protocol->per_session_data_size && !wsi->user_space) {
>        wsi->user_space = malloc(
>                  wsi->protocol->per_session_data_size);
>        if (wsi->user_space  == NULL) {
>            lwsl_err("Out of memory for conn user space\n");
>            return 1;
>        }
>        memset(wsi->user_space, 0,
>                     wsi->protocol->per_session_data_size);
>    }
>    return 0;
>}
>
>
>So, this user data is meant only for storing information across all
>sessions for a particular connection.
>
>Is there a way I can store some user data unrelated to session data e.g
>my thread context information and
>access it inside the callback to identify the thread context under
>which the callback is called?

There is a void * user member in theinfo struct you can prepare to whatever you like at context creation time.

Afterwards, you can use libwebsocket_context_user(context) to get hold of it.

-Andy

>
>Thanks
>Hemant
>
>
>
>
>
>From: Hemant Kumar
>Sent: Thursday, August 13, 2015 3:29 PM
>To: libwebsockets at ml.libwebsockets.org
>Subject: RE: Accessing user data in websocket callback
>
>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<mailto: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
>
>
>
>------------------------------------------------------------------------
>
>_______________________________________________
>Libwebsockets mailing list
>Libwebsockets at ml.libwebsockets.org
>http://ml.libwebsockets.org/mailman/listinfo/libwebsockets




More information about the Libwebsockets mailing list