]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/beast/example/websocket/client/coro-ssl/websocket_client_coro_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, coroutine
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/spawn.hpp>
23 #include <boost/asio/ip/tcp.hpp>
24 #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
46 std::string
const& host
,
47 std::string
const& port
,
48 std::string
const& text
,
49 boost::asio::io_context
& ioc
,
51 boost::asio::yield_context yield
)
53 boost::system::error_code ec
;
55 // These objects perform our I/O
56 tcp::resolver resolver
{ioc
};
57 websocket::stream
<ssl::stream
<tcp::socket
>> ws
{ioc
, ctx
};
59 // Look up the domain name
60 auto const results
= resolver
.async_resolve(host
, port
, yield
[ec
]);
62 return fail(ec
, "resolve");
64 // Make the connection on the IP address we get from a lookup
65 boost::asio::async_connect(ws
.next_layer().next_layer(), results
.begin(), results
.end(), yield
[ec
]);
67 return fail(ec
, "connect");
69 // Perform the SSL handshake
70 ws
.next_layer().async_handshake(ssl::stream_base::client
, yield
[ec
]);
72 return fail(ec
, "ssl_handshake");
74 // Perform the websocket handshake
75 ws
.async_handshake(host
, "/", yield
[ec
]);
77 return fail(ec
, "handshake");
80 ws
.async_write(boost::asio::buffer(std::string(text
)), yield
[ec
]);
82 return fail(ec
, "write");
84 // This buffer will hold the incoming message
85 boost::beast::multi_buffer b
;
87 // Read a message into our buffer
88 ws
.async_read(b
, yield
[ec
]);
90 return fail(ec
, "read");
92 // Close the WebSocket connection
93 ws
.async_close(websocket::close_code::normal
, yield
[ec
]);
95 return fail(ec
, "close");
97 // If we get here then the connection is closed gracefully
99 // The buffers() function helps print a ConstBufferSequence
100 std::cout
<< boost::beast::buffers(b
.data()) << std::endl
;
103 //------------------------------------------------------------------------------
105 int main(int argc
, char** argv
)
107 // Check command line arguments.
111 "Usage: websocket-client-coro-ssl <host> <port> <text>\n" <<
113 " websocket-client-coro-ssl echo.websocket.org 443 \"Hello, world!\"\n";
116 auto const host
= argv
[1];
117 auto const port
= argv
[2];
118 auto const text
= argv
[3];
120 // The io_context is required for all I/O
121 boost::asio::io_context ioc
;
123 // The SSL context is required, and holds certificates
124 ssl::context ctx
{ssl::context::sslv23_client
};
126 // This holds the root certificate used for verification
127 load_root_certificates(ctx
);
129 // Launch the asynchronous operation
130 boost::asio::spawn(ioc
, std::bind(
137 std::placeholders::_1
));
139 // Run the I/O service. The call will return when
140 // the get operation is complete.