[Libwebsockets] R: Newbie: is it possible to geenrate data from server?

Stefano Mora stefano.mora at newdep.com
Fri Sep 28 09:57:11 CEST 2018


Thanks Andy,
I added a lws_cancel_service(pED->context); call in each timed function that wants to send some data to client and a LWS_CALLBACK_EVENT_WAIT_CANCELLED handler.
When the function wants to send data, it fills a global buffer and calls lws_cancel_service.

Now the problem is that I have 3 functions recalled back-to-back from the timer handler and if two of them need to send data, often the second one overwrite the first data before it is sent.
If I have more than one lws_cancel_service call, is there a way to differentiate each of them?

Thanks!

-- Ste

-----Messaggio originale-----
Da: Andy Green <andy at warmcat.com> 
Inviato: venerdì 21 settembre 2018 14:37
A: Stefano Mora <stefano.mora at newdep.com>; libwebsockets at ml.libwebsockets.org
Oggetto: Re: [Libwebsockets] Newbie: is it possible to geenrate data from server?



On 21/09/2018 20:28, Stefano Mora wrote:
> Hi all,
> 
> I'm developing a simple websocket server based on minimal sample.
> 
> No problem when I need to reply to client but I'd like to generate 
> data from server and push them to client.
> 
> Is it possible?

Sure... the client has to reach out first though and establish the ws connection.

> Which wsi structure do I need to point ?

If I understand you, you want the client to connect and then wait until the server decides something exciting happened and send a ws message.

When the server wants to talk to the client, he just has to either call
lws_callback_on_writable() or similar api

https://libwebsockets.org/git/libwebsockets/tree/include/libwebsockets/lws-writeable.h

...if the guy who decides it should happen is on the same thread as the lws service, or if the decision is on another thread, then
lws_cancel_service() to wake the service thread(s) and have them realize there is something to do.

See
https://libwebsockets.org/git/libwebsockets/tree/minimal-examples/ws-server/minimal-ws-server-threads
for an example of the latter.

-Andy

> Thanks!
> 
> 
> _______________________________________________
> Libwebsockets mailing list
> Libwebsockets at ml.libwebsockets.org
> https://libwebsockets.org/mailman/listinfo/libwebsockets
> 


More information about the Libwebsockets mailing list