warmcat / libwebsockets

canonical libwebsockets.org networking library
https://libwebsockets.org
Other
4.77k stars 1.49k forks source link

How to close socket and connection #1463

Closed alexiicon closed 5 years ago

alexiicon commented 5 years ago

Hi, I can't find any correct information about how to close the socket connection from server side? I tried to use lws_close_reason(...,...) but it's not working. I searched through google and others, but no luck. I'm using latest version of libwebsockets and it doesn't have lws_close_and_free_session or lws_close_free_wsi methods.

lws-team commented 5 years ago

lws_close_reason()

This sets metadata to send when the close happens, for ws only. It doesn't close anything.

Normally you close a wsi as a reaction to a callback, by returning nonzero from the callback. Timeouts will also cause the close implicitly when they fire.

Sometimes you need to do it as a reaction to something else. In those cases you can use the timeout path with special flags to force the close.

https://libwebsockets.org/git/libwebsockets/tree/include/libwebsockets/lws-timeout-timer.h#n79-105