[Libwebsockets] Help getting started with data from outside of the client callback loop
andy at warmcat.com
Thu Aug 3 16:51:14 CEST 2017
On August 3, 2017 10:13:32 PM GMT+08:00, Nick Poole <nickpoole at black-sphere.co.uk> wrote:
>I'm still just getting started with LWS, but I'm struggling to get my
>head around how the callback loop is intended to be used in the context
>of a WS client application that is producing data outside of the loop
>(I've already got a basic PoC working within the loop using static
>I think what I'm looking for is a way of passing a reference to a
>message queue into the LWS connection context, so that within the loop,
>the callbacks can pop messages off the queue, serialise them and send
>the WS server, and receive messages to be added to the queue (might
>a send queue and a receive queue). I really want to avoid making these
>The problem is that I can't see how to pass such references into the
When you create the context, you can set struct lws_context_creation_info .user to your "global" object. Then from any callback, you can recover it with
* lws_context_user() - get the user data associated with the context
* \param context: Websocket context
* This returns the optional user allocation that can be attached to
* the context the sockets live in at context_create time. It's a way
* to let all sockets serviced in the same context share data without
* using globals statics in the user code.
LWS_VISIBLE LWS_EXTERN void *
lws_context_user(struct lws_context *context);
Eg struct myobject *mo = lws_context_user(lws_get_context(wsi));
>* there's a per-connection user data object that's malloc'ed by LWS,
>I don't seem to be able to access that from outside the callback. I'm
>also wondering how I should be free'ing any memory malloc'ed within the
>callback and referenced in the user data.
Connections come and go asynchronously and logical management of that is serialized into the event loop. Even for client connections, when or whether they connect and how long they logically exist is not under the control of your code alone, but is discovered later by the event loop / callbacks.
For ws connections allocations would usually be done at LWS_CALLBACK_CLIENT_ESTABLISHED and freed at LWS_CALLBACK_CLOSE.
>* there is a pointer to an object that can be set on the protocol
>struct, but I can't work out how you access that from inside the
>callback, and given that all the examples define the protocols as
>const, I'm not sure whether dynamically building the struct during
>client setup is even the right thing to do.
>Any hints or suggestions will be greatly appreciated.
You also have the option to set your own wsi user pointer for individual client connections when you create it with struct lws_client_connect_info
/**< if non-NULL, use this as wsi user_data instead of malloc it */
It that case you allocate it yourself and need to free it in LWS_CALLBACK_CLOSE and LWS_CALLBACK_CLIENT_CONNECTION_ERROR; lws doesn't allocate or free the user stuff if you gave your own.
>Libwebsockets mailing list
>Libwebsockets at ml.libwebsockets.org
More information about the Libwebsockets