[Libwebsockets] Client close example

Brenton Rothchild brentonr at dorm.org
Mon May 1 17:25:50 CEST 2017

On Tue, Apr 25, 2017 at 5:11 PM, Andy Green <andy at warmcat.com> wrote:

> On April 26, 2017 6:02:25 AM GMT+08:00, Brenton Rothchild <
> brentonr at dorm.org> wrote:
> >Hi,
> >
> >I've tried looking for examples on this list or in documentation, but
> >so
> >far I cannot find a good answer to the following question:
> >
> >Can someone give a good example (simple, single threaded) where the
> >client
> >sends a closing
> >reason to a WS server (any kind) with a reason using
> >lws_close_reason()?
> >
> >I am finding that with v2.1.0 and v2.2.1, I get sporadic results where
> >sometimes callling lws_close_reason() and returning -1 from the
> >callback
> >will not actually send the close message payload and the socket is
> >never
> >closed (as viewed using tcpdump).
> >
> >I'm fairly positive I'm simply not using the library correctly, and
> >would
> >very much appreciate a
> >concrete example where the client is closing the connection with a
> >close
> >reason.
> Run libwebsockets-test-server, visit http://localhost:7681, go to the
> 'close testing' tab and use the open and close buttons there.  The sources
> are in ./test-server.
> There are no guarantees the peer hasn't already closed the tcp connection,
> and it's legal for either side to close the connection with no warning at
> any time.
> If there's a gap in the close logic under some circumstances, you'll need
> to capture more info on how to reproduce.
> -Andy
> >Thanks!
> >-Brenton

Andy, thanks for the reply.

I found the problem with my client. Although the documentation states
returning -1 from the callback in any case will cause the connection to
I found I was hitting a case where the return value does not cause this

The source of this was in lws-plat-unix.c, function _lws_plat_service_tsi()
when calling with reason LWS_CALLBACK_GET_THREAD_ID where
the return value is not treated as a possible error at that point.

(This was contrary to the similar call to the callback with
reason LWS_CALLBACK_GET_THREAD_ID in pollfd.c, function _lws_change_pollfd()
where a thread ID of -1 does in fact trigger closing the connection.)

I adjusted my callback to return -1 when the reason value was any different
value, and it worked great.

I assume again my fault is not using the library correctly as a whole, but
this got me past my current issue.

Thanks again for your help,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://libwebsockets.org/pipermail/libwebsockets/attachments/20170501/b3cfc65e/attachment.html>

More information about the Libwebsockets mailing list