[Libwebsockets] Help getting started with data from outside of the client callback loop

Andy Green andy at warmcat.com
Thu Aug 3 17:36:37 CEST 2017

On August 3, 2017 11:10:29 PM GMT+08:00, Nick Poole <nickpoole at black-sphere.co.uk> wrote:
>Thanks Andy.
>> You also have the option to set your own wsi user pointer for
>individual client connections when you create it with struct
>> void *userdata;
>> /**< 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
>doesn't allocate or free the user stuff if you gave your own.
>I hadn't realised it was possible to pass the per-connection user-data 
>in like that: I'd only seen the reference to how the protocol
>defines the size to malloc it to. I assume I can free it after I've 
>called lws_context_destroy()?

Yes if you provide it, you free it.  The internal wsi that holds a pointer to it is itself freed when the wsi closes or realizes the connection attempt failed; all wsi are closed and freed as part of destroying the context.  So there is nobody left from lws side able to dereference it and no way to get a problem.

It's generally 'better' / more scaleable (other users mix n client connections with server connections in the same context) to allocate it on the connect attempt and free it when you see CONNECTION_ERROR or CLOSE, but if you know you only have one client connection with a fixed role for the lifetime of whatever you're trying to do it's also ok.


>Thanks again,

More information about the Libwebsockets mailing list