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

Andy Green andy at warmcat.com
Fri Sep 21 14:37:24 CEST 2018



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