Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

libwebsockets: How to close connection

Tags:

c

websocket

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

I know I can return a negative int in my callback function to make the connection abort, but that feels more like something that happens on an error. In some example code on some random blog, I found a reference to a function libwebsocket_close_and_free_session but that does not exist (any longer?) in the library code.

Ideally, I would like to be able to close a different connection from within the callback handler than the one being called (i.e. to interpret a command like “close the other connection”).

like image 224
poke Avatar asked Jul 01 '13 11:07

poke


1 Answers

In the previous version of the libwebsockets the public API had the libwebsocket_close_and_free_session function.

The previous test code used that to close the connections e.g.

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 that's the intended graceful approach

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

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

like image 143
Gerard Condon Avatar answered Sep 25 '22 22:09

Gerard Condon