[Libwebsockets] How to send data asynchronously to the client?
lws14 at mordent.com
Mon Jul 7 14:38:02 CEST 2014
This is very similar to what I've been doing. I don't handle
CALLBACK_GET_THREAD_ID, but I use cancel in my "queue a message"
function and do the callback_on_writeable exactly as you do in the
service loop. I never callback_on_writeable in another thread because
that did not seem to ever work and wasn't thread safe (I looked at the
source to make sure). So far it's actually working mostly, except
sometimes I get a crash in callback_on_writeable. In your code, you
don't handle partial writes, or write errors. You will probably need to
handle partial writes or you will get garbage on the other end. For
that what I did was something like this (buf is my padded copy of msg):
int stat = libwebsocket_write( ws, buf, msg.length(), LWS_WRITE_TEXT );
// If we got an error writing bag out and make sure the socket gets
if( stat < 0 )
if( ( unsigned int )stat < msg.length() )
CThreadMutexLocker lock( &s_messagesmutex );
m_messages[ws].push_front( msg.substr( stat ) );
libwebsocket_callback_on_writable( context, ws );
I'm still trying to track down my crash. It can't be a thread stomping
issue because the only thing calling into libwebsockets is my service
loop. Somehow my ws must be getting freed.
On 07/06/2014 06:09 AM, Bas Appelo wrote:
> I still have problems calling libwebsocket_callback_on_writable from
> another thread, calling libwebsocket_cancel_service works better.
> I wrote an little c++ program with the intention of showing where my
> problem shows up:
> It includes a Makefile that works on my mac, building should not be
> too difficult.
> It works fine as it is, but contains two defines in main on line 6 and
> WebSocket line 5, if uncommented two problems will show.
> The first is that if I change from calling cancel from the foreign
> thread to calling libwebsocket_callback_on_writable directly from this
> foreign thread I'm not actually receiving a callback.
> I added the mentioned check thread ID callback, but this did not help.
> The other problem is that if I create a second instance
> of libwebsocket_context I also stop receiving callbacks.
> Could you check if I'm using libwebsocket decently in this example? At
> least the cancel_service part works nice, why the other two issues are
> there I don't know, could be my misunderstanding of how to use
> libwebsocket. Worth to mention I'm on a Mac(that could be the issue),
> I think up to date in most important parts, at least on the
> libwebsocket code.
> If you like my approach, or at least the basis case, I'd be happy to
> add some comments and make it really thread-safe[I kinda brutally cut
> this from my project code] so it could serve as an example for the
> questions I see every now and then about threading and libwebsocket.
> Libwebsockets mailing list
> Libwebsockets at ml.libwebsockets.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Libwebsockets