1.

How Do I Negotiate Subprotocols?

Answer»

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.

WARNING: Asio's io_service has a method called stop. WebSocket++ wraps this method as websocketpp::transport::asio::endpoint::stop. While this operation has a benign sounding name, it is a powerful and destructive operation that should only be used in special cases. If you are using io_service::stop or endpoint::stop without a very good reason your program is likely BROKEN and may exhibit erratic behavior. SPECIFICALLY, io_service::stop stops the processing of events entirely. This does not give current operations (such as socket closing handshakes) the opportunity to finish. It will leave your sockets in a dangling state that may invoke operating system level timeouts or other errors.

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:

WARNING: Asio's io_service has a method called stop. WebSocket++ wraps this method as websocketpp::transport::asio::endpoint::stop. While this operation has a benign sounding name, it is a powerful and destructive operation that should only be used in special cases. If you are using io_service::stop or endpoint::stop without a very good reason your program is likely broken and may exhibit erratic behavior. Specifically, io_service::stop stops the processing of events entirely. This does not give current operations (such as socket closing handshakes) the opportunity to finish. It will leave your sockets in a dangling state that may invoke operating system level timeouts or other errors.



Discussion

No Comment Found