[Libwebsockets] libwebsocket close connection manually

Andy Green andy at warmcat.com
Tue Sep 23 14:44:30 CEST 2014



On 23 September 2014 15:46:46 GMT+08:00, Dnyanesh Gate <dnyanesh.gate at intelli-vision.com> wrote:
>Hello Andy,
>
>While using libwebsocket library, there are some cases where I have to
>restart websocket connection. But library doesn't expose any API for
>disconnecting specific websocket connection.

I'm not sure if we're being the server or the client.

>I am creating only one libwebsocket context and creating multiple
>websocket connection using same context. But now I wish to disconnect
>one one of the connection. By destroying context it will close all
>connections.
>
>To fix my problem I have added one API in libwebsocket.h file
>
>void libwebsocket_close_and_free_session2(struct libwebsocket_context
>*context, struct libwebsocket *wsi) {
>    libwebsocket_close_and_free_session(context, wsi,
>LWS_CLOSE_STATUS_NOSTATUS);
>}
>
>Do you have any suggestion on this?

Lws approach is

 - don't use a wsi outside the user callback.  You can do it in one case, async call of 'callback when writesble' api, if you hook established and closed callbacks to follow wsi lifecycle.

 - return nonzero from the user callback to close the connection

The reason is the only time you are guaranteed the wsi is not deleted is during a callback on that wsi.

If your problem is something happened to the connection such that he's never going to call back as writeable, you can use the ping / pong control packets and / or timeout apis directly to get the library to do the close cleanly automatically.

-Andy

>--
>Thanks & Regards,
>DnyaneshG.




More information about the Libwebsockets mailing list