Zohar81
Zohar81

Reputation: 5074

Find the best way to reconnect Boost::Beast websocket

I'd like get an advice about how to replace boost::beast based ssl websocket connection between 2 different servers that will minimize the reconnection time. my web socket client object is from the following type as a member variable of std::optional<websocketMgr> mgr; and it defined as follows :

class websocketMgr { 

...

private:

   boost::beast::websocket::stream<
    boost::beast::ssl_stream<boost::beast::tcp_stream>> ws_;
}

during steady state, the web socket may send asnyc_write calls and is halting in read ws_.async_read(buffer, yield);

From time to time, I'd like to change the connection to a different server peer. So I first need to trigger an exception on the async_read that is waiting by the current socket thread - this will eventually reach the socket d'tor and close the current connection before setting a new one.

void websocketMgr::closeWebsocket() {
ws_.close(boost::beast::websocket::close_code::normal);

...

mgr.emplace(...); // create new websocket, after the older one fully removed 

However, as I experienced, the ssl_stream (underlying object) can take very long time to complete since it may reach timeout if the server side is currently using the connection and Is not cooperating in the shutdown process.

Perhaps anyone can suggest a way that will ensure that the older connection is closed gracefully on the background, while the new connection is created as fast as possible to reduce lack of connectivity.

Thanks !

Upvotes: 1

Views: 309

Answers (0)

Related Questions