]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/beast/example/websocket/client/async-ssl/websocket_client_async_ssl.cpp
2 // Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // Official repository: https://github.com/boostorg/beast
10 //------------------------------------------------------------------------------
12 // Example: WebSocket SSL client, asynchronous
14 //------------------------------------------------------------------------------
16 #include "example/common/root_certificates.hpp"
18 #include <boost/beast/core.hpp>
19 #include <boost/beast/websocket.hpp>
20 #include <boost/beast/websocket/ssl.hpp>
21 #include <boost/asio/connect.hpp>
22 #include <boost/asio/ip/tcp.hpp>
23 #include <boost/asio/ssl/stream.hpp>
30 using tcp
= boost::asio::ip::tcp
; // from <boost/asio/ip/tcp.hpp>
31 namespace ssl
= boost::asio::ssl
; // from <boost/asio/ssl.hpp>
32 namespace websocket
= boost::beast::websocket
; // from <boost/beast/websocket.hpp>
34 //------------------------------------------------------------------------------
38 fail(boost::system::error_code ec
, char const* what
)
40 std::cerr
<< what
<< ": " << ec
.message() << "\n";
43 // Sends a WebSocket message and prints the response
44 class session
: public std::enable_shared_from_this
<session
>
46 tcp::resolver resolver_
;
47 websocket::stream
<ssl::stream
<tcp::socket
>> ws_
;
48 boost::beast::multi_buffer buffer_
;
53 // Resolver and socket require an io_context
55 session(boost::asio::io_context
& ioc
, ssl::context
& ctx
)
61 // Start the asynchronous operation
68 // Save these for later
72 // Look up the domain name
73 resolver_
.async_resolve(
79 std::placeholders::_1
,
80 std::placeholders::_2
));
85 boost::system::error_code ec
,
86 tcp::resolver::results_type results
)
89 return fail(ec
, "resolve");
91 // Make the connection on the IP address we get from a lookup
92 boost::asio::async_connect(
93 ws_
.next_layer().next_layer(),
99 std::placeholders::_1
));
103 on_connect(boost::system::error_code ec
)
106 return fail(ec
, "connect");
108 // Perform the SSL handshake
109 ws_
.next_layer().async_handshake(
110 ssl::stream_base::client
,
112 &session::on_ssl_handshake
,
114 std::placeholders::_1
));
118 on_ssl_handshake(boost::system::error_code ec
)
121 return fail(ec
, "ssl_handshake");
123 // Perform the websocket handshake
124 ws_
.async_handshake(host_
, "/",
126 &session::on_handshake
,
128 std::placeholders::_1
));
132 on_handshake(boost::system::error_code ec
)
135 return fail(ec
, "handshake");
139 boost::asio::buffer(text_
),
143 std::placeholders::_1
,
144 std::placeholders::_2
));
149 boost::system::error_code ec
,
150 std::size_t bytes_transferred
)
152 boost::ignore_unused(bytes_transferred
);
155 return fail(ec
, "write");
157 // Read a message into our buffer
163 std::placeholders::_1
,
164 std::placeholders::_2
));
169 boost::system::error_code ec
,
170 std::size_t bytes_transferred
)
172 boost::ignore_unused(bytes_transferred
);
175 return fail(ec
, "read");
177 // Close the WebSocket connection
178 ws_
.async_close(websocket::close_code::normal
,
182 std::placeholders::_1
));
186 on_close(boost::system::error_code ec
)
189 return fail(ec
, "close");
191 // If we get here then the connection is closed gracefully
193 // The buffers() function helps print a ConstBufferSequence
194 std::cout
<< boost::beast::buffers(buffer_
.data()) << std::endl
;
198 //------------------------------------------------------------------------------
200 int main(int argc
, char** argv
)
202 // Check command line arguments.
206 "Usage: websocket-client-async-ssl <host> <port> <text>\n" <<
208 " websocket-client-async-ssl echo.websocket.org 443 \"Hello, world!\"\n";
211 auto const host
= argv
[1];
212 auto const port
= argv
[2];
213 auto const text
= argv
[3];
215 // The io_context is required for all I/O
216 boost::asio::io_context ioc
;
218 // The SSL context is required, and holds certificates
219 ssl::context ctx
{ssl::context::sslv23_client
};
221 // This holds the root certificate used for verification
222 load_root_certificates(ctx
);
224 // Launch the asynchronous operation
225 std::make_shared
<session
>(ioc
, ctx
)->run(host
, port
, text
);
227 // Run the I/O service. The call will return when
228 // the get operation is complete.