[Libwebsockets] Send messages to client from Server

Andy Green andy at warmcat.com
Thu Sep 25 09:22:27 CEST 2014



On 25 September 2014 14:56:44 GMT+08:00, SriHarsha <mynameissriharsha at gmail.com> wrote:
>Thank you so much for the quick response.
>
>So I guess the logic should be queue up the messages that need to be
>sent
>and whenever you get a LWS_CALLBACK_SERVER_WRITEABLE you serialize the
>messages and send them to the client ?

It depends on where the messages come from, but that sounds like what you would usually want.

If the amount of buffered messages gets big, you might want some policy to drop the older messages, or if it gets big enough stop creating new data or drop the connection since it can't handle what you're trying to send.

-Andy

>
>On Wed, Sep 24, 2014 at 11:51 PM, Andy Green <andy at warmcat.com> wrote:
>
>>
>>
>> On 25 September 2014 14:46:39 GMT+08:00, SriHarsha <
>> mynameissriharsha at gmail.com> wrote:
>> >Hey Andy,
>> >How are you ?
>> >
>> >I had a quick question about how to send messages from the server to
>> >the
>> >browser.
>> >
>> >Right now I am hoping when I receive a connection in my callback
>> >LWS_CALLBACK_ESTABLISHED
>> >I can store the pointer to the libWebSocket struct and use it later
>on.
>> >
>> >When I want to send the message from the server to my client I want
>to
>> >use libwebsocket_write
>> >and then pass the pointer that I had stored previous.
>> >
>> >Do you think this is a nice way to do it ?
>>
>> No, the library does not work like that.
>>
>> Look at the example apps and how they ask for a callback when the
>> connection is writeable, and do any writing from that callback.
>>
>> You can keep a wsi by following the lifecycle callbacks, but unless
>you
>> have another thread that wants it (and he is only allowed to ask for
>a
>> callback when it is writeable), you do not need to.
>>
>> -Andy
>>
>> >Any help is greatly appreciated.
>> >
>> >Thank you,
>> >H
>>
>>




More information about the Libwebsockets mailing list