File indexing completed on 2025-09-17 08:38:23
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef BOOST_MQTT5_WEBSOCKET_HPP
0009 #define BOOST_MQTT5_WEBSOCKET_HPP
0010
0011 #include <boost/mqtt5/detail/async_traits.hpp>
0012 #include <boost/mqtt5/detail/shutdown.hpp>
0013
0014 #include <boost/mqtt5/types.hpp>
0015
0016 #include <boost/beast/http/field.hpp>
0017 #include <boost/beast/websocket/rfc6455.hpp>
0018 #include <boost/beast/websocket/stream.hpp>
0019
0020 namespace boost::mqtt5 {
0021
0022
0023 template <typename Stream>
0024 struct ws_handshake_traits<boost::beast::websocket::stream<Stream>> {
0025
0026 template <typename CompletionToken>
0027 static decltype(auto) async_handshake(
0028 boost::beast::websocket::stream<Stream>& stream,
0029 authority_path ap, CompletionToken&& token
0030 ) {
0031 using namespace boost::beast;
0032
0033
0034 stream.set_option(
0035 websocket::stream_base::timeout::suggested(role_type::client)
0036 );
0037
0038 stream.binary(true);
0039
0040
0041 stream.set_option(websocket::stream_base::decorator(
0042 [](websocket::request_type& req) {
0043 req.set(http::field::sec_websocket_protocol, "mqtt");
0044 req.set(http::field::user_agent, "boost.mqtt");
0045 })
0046 );
0047
0048 stream.async_handshake(
0049 ap.host + ':' + ap.port, ap.path,
0050 std::forward<CompletionToken>(token)
0051 );
0052 }
0053 };
0054
0055 namespace detail {
0056
0057
0058 template <typename Stream, typename ShutdownHandler>
0059 void async_shutdown(
0060 boost::beast::websocket::stream<Stream>& stream, ShutdownHandler&& handler
0061 ) {
0062 stream.async_close(
0063 beast::websocket::close_code::normal,
0064 std::move(handler)
0065 );
0066 }
0067
0068 }
0069
0070 }
0071
0072 #endif