Open NationYY opened 2 years ago
https://docs.websocketpp.org/faq.html
How do I cleanly exit an Asio transport based program The Asio transport based clients and servers use the Asio library's underlying io_service to handle asyncronous networking operations. The standard behavior of the io_service is to run until there are no async operations left and then return. WebSocket++, when using the Asio transport, behaves like a standard Asio application. If you want your WebSocket++/Asio based program to stop network operations and cleanly close all sockets you will want to do the following:
For servers, call websocketpp::transport::asio::endpoint::stop_listening to initiate the closing of the server listening socket. For clients, if you have engaged perpetual mode with websocketpp::transport::asio::endpoint::start_perpetual, disable it with websocketpp::transport::asio::endpoint::stop_perpetual. For both, run websocketpp::endpoint::close or websocketpp::connection::close on all currently outstanding connections. This will initiate the WebSocket closing handshake for these connections Wait. Asio is asyncronous. When the calls to the above methods (stop_listening, close, etc) complete the server will still be listening, the connections will still be active until the io_service gets around to asyncronously processing the socket and WebSocket protocol closing handshakes. The io_service::run method will exit cleanly and automatically when all operations are complete.
i usually use "void endpoint<connection,config>::close(connection_hdl hdl, close::status::value const code, std::string const & reason)" like "m_endpoint.close(m_hdl,websocketpp::close::status::going_away,"")" most time it's success. but sometime, the websocketpp::client::run not break, so my thread is always block