Libwebsockets: how to close a connection

Im uses libwebsockets to run both the websocket server and the client (actually in the same executable). Is there a way to gracefully close the connection?

I know that I can return a negative int in my callback function to disconnect the connection, but this is more like what happens when an error occurs. In some code example on some random blog, I found a link to the libwebsocket_close_and_free_session function, but it does not exist (anymore?) In the library code.

Ideally, I would like to be able to close another connection from the callback handler than the one that is being called (that is, interpret a command like "close another connection").

+4
source share
1 answer

In the previous version of libwebsockets, the open API had the libwebsocket_close_and_free_session function.

The previous test code is used to close connections, for example.

 if (mirror_lifetime == 0) { fprintf(stderr, "closing mirror session\n"); libwebsocket_close_and_free_session(context, wsi_mirror, LWS_CLOSE_STATUS_GOINGAWAY); 

but the new test code uses the return -1 approach, so I assume the supposed graceful approach

 mirror_lifetime--; if (!mirror_lifetime) { fprintf(stderr, "closing mirror session\n"); return -1; } 

The function still exists in the library code, but now it is in the private-libwebsocket.h . I'm not sure if you can access it (or access it) from there in your code. If you could, this would be the perfect API for you, as you can close other connections if you have a struct libwebsocket *wsi .

+5
source

Source: https://habr.com/ru/post/1489083/


All Articles