[Libwebsockets] What is the correct way to close a WS client connection?
andy at warmcat.com
Sat May 9 08:00:11 CEST 2020
On 5/9/20 6:44 AM, Olivier Langlois wrote:
> 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
>>> I want to implement an inactivity timer because my WS client has
>>> 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
>> 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
>>> shutdown the process and clean-up lws, that is the only way that I
>>> currently initiate a connection shutdown. Beside that unique
>>> I simply react passively to events received in the callback to
>>> reconnections when lws reports a connection error or that the
>>> close the connection.
>>> What is the function to call to let lws know that we want to close
>>> a ws
>>> client socket?
> 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.
Yes it's new in v4.0.
> 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.
It's going to catch most cases but it doesn't provide any assurance that
what you send the server is getting to it. In your application there
may be nothing you send to the server so you don't care, but the
situation the kernel is still accepting small packets from userland for
send on a tcp connection that isn't actually passing anything is not
that hard to arrive at. The validity stuff will winkle it out.
What most people do for this kind of thing is just use lws_set_timeout()
to keep resetting the connection timeout to something like 3s or
whatever every time they receive something. It will deal itself with
noticing the timeout expired and autoclose the connection. That's been
around for many years.
> 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