[Libwebsockets] How to "disconnect"?

Andy Green andy at warmcat.com
Thu Aug 14 14:23:04 CEST 2014

On 14 August 2014 20:00:37 GMT+08:00, "Carlos Sánchez" <carlos at guadalfeo.com> wrote:
>> What version of lws are you using?  Because there was a fix about
>this recently -->
>Ok, I don't have this version. I've updated my sources.
>>> |Values are not changed, but I don't know if they should be updated
>>> with
>>> libwebsocket changes.| I've also changed this /proc values manually
>>> still does not detect the connection lost.
>>> ||
>>>> About the client lifecycle part of the question the test client
>>> something similar, he repeatedly closes the connection after a
>>> time (by returning nonzero from a callback) and when he sees he's
>>> in the main service loop, just calls client connect again to get a
>>> connection.  The context is unaffected.
>>>> So you should be able to do what uou're looking for the same way.
>>> The situation I've described, the connection is not destroyed (no
>>> LWS_CALLBACK_WSI_DESTROY) so libwebsockets does not known that the
>>> connection is lost and I can't call libwebsocket_client_connect()
>>> before destroying the previous connection (memory leaks). So the
>>> solution I've found is destroying the context because I can't inform
>>> libwebsockets that I've detected no pong and needs to free all
>>> resources
>>> allocated by the connection.
>> If tcp keepalive is workable you should get the connection closed
>Ok, now works with keepalive working.


>> If you are sending the ping from your user callback, you can also use
>the lws timeout setting api from there and clear the timeout when you
>see a pong.  Don't see a pong --> timeout --> close.
>Now there is no need for this, but for curiosity as you mention I must 
>do this?:
>     libwebsocket_set_timeout(wsi, PENDING_TIMEOUT_AWAITING_PING, 0);
>     libwebsocket_service_fd(, NULL);
>I'm calling from a libev timer callback so as I understand I must

I mean at the time your server decides to send a ping "probe", which like anything you do must be done from the user callback code, you should set the timeout on the connection to 5s or whatever.  So like you have above except not 0 by 5 or whatever.

There is no explicit service needed, when you return from the callback after doing that he'll end up back in your main service loop and go around again / track timeouts without doing anything more.

Then you will either get a PONG callback from the client before then, at which time you should disable the pending timeout with libwebsockets_set_timeout(wsi, NO_PENDING_TIMEOUT, 0); or if it never came, lws will close the connection for you.

lws client code automatically sends back a PONG if it gets a PING.


>> -Andy
>>> Thanks,
>>> Carlos
>>>> -Andy
>>>>> -m
>>>>> On 07/29/2014 11:44 AM, Carlos Sánchez wrote:
>>>>>> I've make a client application that is working fine (since now)
>>>>>> testing communication issues I've reached a point where I can't
>>> solve
>>>>>> a connection problem.
>>>>>> Once connected and after many data exchange without problems,
>>>>>> disconnected the device from the internet without loosing the
>>>>>> (I've removed the "output" cable of the switch where device is
>>>>>> connected). So, there is no link down detection and libwebsocket
>>>>>> doesn't detect the disconnection from the server. After two
>>> minutes,
>>>>>> my "ping" timer fires and sends a LWS_WRITE_PING without any
>>>>> and
>>>>>> of course after another one minute the "pong" timer fires and
>>> detect
>>>>>> the missing server connection.
>>>>>> I need to "disconnect" and "connect" again ("reconnect"), but I
>>> can't
>>>>>> found the analogous function of libwebsocket_client_connect().
>>>>>> How can I force a "disconnect" without destroying the context,
>>> if
>>>>>> possible reuse the connection?
>>>>> _______________________________________________
>>>>> Libwebsockets mailing list
>>>>> Libwebsockets at ml.libwebsockets.org
>>>>> http://ml.libwebsockets.org/mailman/listinfo/libwebsockets
>>>> _______________________________________________
>>>> Libwebsockets mailing list
>>>> Libwebsockets at ml.libwebsockets.org
>>>> http://ml.libwebsockets.org/mailman/listinfo/libwebsockets

More information about the Libwebsockets mailing list