1

I'm developing a client using the venereable libwebsockets library.

I don't know where to assign a char * (somewhere during setup I presume) I have so that I can get my hands on the same via the void *user parameter in my client's lws_callback_function().

I've tried setting user in my lws_protocols, I've tried setting userdata in my struct lws_client_connect_info, and a bunch of other places I can't remember.

QED
  • 9,803
  • 7
  • 50
  • 87
  • I'm not sure I understand the question... what are you trying to do? what did you try? where are you stuck? ... what does it mean "get a `char *` through to the `void *user` parameter...? Show us some code! – Myst Feb 03 '18 at 22:51
  • Please show us some code? For example, I suspect the `user` parameter is read only and allocated by the library when the connection is initialized, so I'm not sure what you're trying to do. – Myst Feb 04 '18 at 00:29
  • @Myst I think that's true when the library is used as a server, but I'm using it as a client. And I don't feel the need to post any code. The links into libwebsockets.org are there, and my question should be clear to anybody who has implemented a callback before. Mind you this is not a websockets question – this is a library question. – QED Feb 04 '18 at 02:31
  • Minimal, Complete, and Verifiable example to show us where you're stuck ... see [How to Ask](https://stackoverflow.com/help/how-to-ask). I'm trying to help. – Myst Feb 04 '18 at 05:43
  • I appreciate that but I think the question is OK. Anybody who can answer it will know what I'm after. – QED Feb 05 '18 at 17:04

2 Answers2

3

I got this to work, but took a bit of investigation..

  1. stash user data in lws_context_creation_info:
    struct app_data_t { int something; };
    app_data_t app_data;
    app_data.something = 123456;
    ...
    lws_context_creation_info lws_cx_config; 
    ... 
    lws_cx_config.user = &app_data;
    ... 
    lws_context * lws_cx = lws_create_context(&lws_cx_config);
  1. recover user data from callback like this:
    int callback(struct lws * wsi,
                 lws_callback_reasons reason,
                 void * pss_user_data, void * in, size_t len)
    {
        lws_context * lws_cx = lws_get_context(wsi);
        app_data_t * app_data = (app_data_t *)(lws_context_user(lws_cx));

        lwsl_user("callback: enter: something=%d", app_data->something);

        ...
    }
Marius
  • 3,372
  • 1
  • 30
  • 36
1

I guess what you want is: a private data for every connection ?

A. If you just need to allocate a piece of memory, you can try this:

struct lws_protocols::per_session_data_size = 1024 bytes;
struct lws_context_creation_info::protocols = protocols;

and then you will get it in lws_callback(* user), user is a private 1024 bytes memory block.

B. If you need to pass a parameter and use it :

lws_client_conn_info.opaque_user_data = &mydata;

in lws_callback(*wsi) :

MyData *my_private_data = (MyData*)lws_get_opaque_user_data(wsi);
gary133
  • 301
  • 1
  • 6