[Libwebsockets] What is the correct way to close a WS client connection?

Olivier Langlois olivier at olivierlanglois.net
Sat May 9 07:44:52 CEST 2020

On Fri, 2020-05-08 at 23:05 +0000, andy at warmcat.com wrote:
> On May 8, 2020 9:44:34 PM UTC, Olivier Langlois <
> olivier at olivierlanglois.net
> > wrote:
> > I want to implement an inactivity timer because my WS client has
> > just
> > sit idle since I have rebooted my router at home yesterday evening.
> > I want lws to initiate a WS client close so that the lws callbacks
> > trigger my reconnection code.
> Lws has a smart 'connection validity' management that will provoke
> PING type probing if it hasn't seen something prove the connection is
> valid for some time.  See the second part here
> https://libwebsockets.org/git/libwebsockets/tree/READMEs/README.lws_retry.md
> Since either side can fail in a sneaky way you can only go off
> tranactions that can only happen if the peer both must have received
> your rx and you can receive his rx, ie, a ping / pong.  If ypu have
> your own bidi transactions happening, you can report them and
> optimize how many ping probes you're generating that way.
> > By trying to plan how I'll do that, I have figure that beside when
> > I
> > shutdown the process and clean-up lws, that is the only way that I
> > currently initiate a connection shutdown. Beside that unique
> > situation,
> > I simply react passively to events received in the callback to
> > process
> > reconnections when lws reports a connection error or that the
> > server
> > close the connection.
> > 
> > What is the function to call to let lws know that we want to close
> > a ws
> > client socket?
> https://libwebsockets.org/git/libwebsockets/tree/include/libwebsockets/lws-timeout-timer.h#n77-101


This is what I need.

The 'connection validity' feature appears to be very cool. Correct me
if I'm wrong but this is a new feature in v4. I guess this feature is a
good reason to upgrade to v4.

That being said, my WS application is unidirectional streaming from
server to client. Server provides a 1s interval heartbeat in case it
has nothing else to transmit.

Therefore, simply detecting that the connection has been idle for few
seconds is sufficient to declare the connection dead and close it.

Sending a ping would simply delay the inevitable in my context, I

In any case, thx a lot for your help!

More information about the Libwebsockets mailing list