[Libwebsockets] How to send messages frequently ?

hanguofu hanguofu at 163.com
Sun Jan 22 15:57:51 CET 2017

At 2017-01-22 07:38:03, "Andy Green" <andy at warmcat.com> wrote:
>On 22 January 2017 04:15:02 GMT+08:00, hanguofu <hanguofu at 163.com> wrote:
>>I develop an application based on example libwebsockets-test-client.
>>The application has 2 threads : in main thread there is routine
>>callback_dumb_increment  and the message loop : lws_service(context, 50
>>At the other thread , the application send messages with
>>Unfortunately , the interval of message vary much and it may be as 
>>short as 1 millisecond sometimes.
>On the network, with tcp, you are not really in control of sending stuff like that.
>Once the tcp window is filled, your kernel will not accept to buffer anything more, let alone send it.
>The reality is the receiver and every step between decides when you may send something.  You can 'have something new to send every ms' but there's nothing to say you will be able to send it within any time limit.
>So there's maybe a conceptual problem here between how tcp network programming really works and how you are thinking about what you are trying to do.  Udp lets you do this kind of sending but ws is tcp only, and udp offers no guarantee your packets will actually arrive.
Hi , Andy : 
My application may generates the sending messages at short interval , but the through-put is quite low : 1024byte/s at most.
Both large latency  and send-in-wrong-order are O.K. to the messages , as long as they reach the other side completely.  
>>Is it a must to change the message loop interval to 
>>lws_service(context, 1 ) ? I found out some messages are dropped 
>>without any notice if  the "Timeout for poll" is large .
>What is your platform and lws version?
>Are we using ssl?
>What do we mean 'messages are dropped', it means the ones you are sending?
I am running the test in win10 and the lws version is v2.1.0
Currently I only play with ws and in the future I will need ssl.
Yes . I meant the sending messages . Before the application thread calls  lws_callback_on_writable , 
it will print out the sending message. Likewise , the callback function LWS_CALLBACK_CLIENT_WRITEABLE
at the main thread will print out the sending message too.
Unfortunately , some of the sending messages never appears at the print-out of main thread , 
which means they are dropped btwn. the two threads.
I am wondering if it is the right way to feed lws routine with the sending messages from the other thread:
I create my own per_session_data structure :
struct per_session_data__dumb_increment {
 char msg_to_other[1024] ; // this is the message sent to peer.

and pass the pointer to this per_session_data__dumb_increment  and the pointer to the wsi ( only 1 wsi in the whole app. )
to the applicaion thread so that the thread can call lws_callback_on_writable .
When the application wants to send a message, it will fill out the per_session_data__dumb_increment and calls 
lws_callback_on_writable at the other thread. 
Could you please shed some light on it ?

Thankyou !

Guofu Han
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://libwebsockets.org/pipermail/libwebsockets/attachments/20170122/bf7b6cc8/attachment-0002.html>

More information about the Libwebsockets mailing list