I am using boost beast for making websocket connections, a process of mine may have a large number of streams/connections and while terminating the process I am calling blocking close of each websocket in destructor using :
if ( m_ws.next_layer().next_layer().is_open() )
{
boost::system::error_code ec;
m_ws.close(boost::beast::websocket::close_code::normal, ec);
}
Having a lot of websockets, makes terminating the process blocking for a long time, is there a way to force terminate(delete) a connection and free up underlying resources faster? Thanks in advance.
As I told you in the comments, closing the connection would be a fast operation on sockets but it doesn't take time and block the thread. In your case, I don't know how much work your program does to close each socket, but keep in mind that if your main thread ends, which means that the process has ended, the SO releases all the resources that it has been using, without closing each socket, I use this technic, and the WebSocket clients detect the end of the connections, but I close the socket when I have some problems with the protocol or the remote endpoint has been disconnected abruptly.
It could be useful that you share your code to see what other activities your code is doing.
By the way, let me share with you my code, where I have no problem to close websocket:
In my case, I'm using asynchronous methods to read and synchronous methods to write, I'm not using an asynchronous method to write to avoid the scenery of two simultaneous write operations.
Also, it's important to say that I'm using the asynchronous way to accept a new connection.
The code to accept the socket connection, where you can set the TIMES OUTS to write and read, instead of using timers.
The code to read:
The code to write:
If you want to see the whole code, this is the Link. The project is still in the developer phase, but it works.