]>
Commit | Line | Data |
---|---|---|
b32b8144 | 1 | // |
92f5a8d4 | 2 | // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com) |
b32b8144 FG |
3 | // |
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) | |
6 | // | |
7 | // Official repository: https://github.com/boostorg/beast | |
8 | // | |
9 | ||
10 | //------------------------------------------------------------------------------ | |
11 | // | |
12 | // Example: WebSocket SSL client, coroutine | |
13 | // | |
14 | //------------------------------------------------------------------------------ | |
15 | ||
16 | #include "example/common/root_certificates.hpp" | |
17 | ||
18 | #include <boost/beast/core.hpp> | |
92f5a8d4 | 19 | #include <boost/beast/ssl.hpp> |
b32b8144 FG |
20 | #include <boost/beast/websocket.hpp> |
21 | #include <boost/beast/websocket/ssl.hpp> | |
b32b8144 | 22 | #include <boost/asio/spawn.hpp> |
b32b8144 FG |
23 | #include <cstdlib> |
24 | #include <functional> | |
25 | #include <iostream> | |
26 | #include <string> | |
27 | ||
92f5a8d4 TL |
28 | namespace beast = boost::beast; // from <boost/beast.hpp> |
29 | namespace http = beast::http; // from <boost/beast/http.hpp> | |
30 | namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp> | |
31 | namespace net = boost::asio; // from <boost/asio.hpp> | |
32 | namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp> | |
33 | using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp> | |
b32b8144 FG |
34 | |
35 | //------------------------------------------------------------------------------ | |
36 | ||
37 | // Report a failure | |
38 | void | |
92f5a8d4 | 39 | fail(beast::error_code ec, char const* what) |
b32b8144 FG |
40 | { |
41 | std::cerr << what << ": " << ec.message() << "\n"; | |
42 | } | |
43 | ||
44 | // Sends a WebSocket message and prints the response | |
45 | void | |
46 | do_session( | |
47 | std::string const& host, | |
48 | std::string const& port, | |
49 | std::string const& text, | |
92f5a8d4 | 50 | net::io_context& ioc, |
b32b8144 | 51 | ssl::context& ctx, |
92f5a8d4 | 52 | net::yield_context yield) |
b32b8144 | 53 | { |
92f5a8d4 | 54 | beast::error_code ec; |
b32b8144 FG |
55 | |
56 | // These objects perform our I/O | |
92f5a8d4 TL |
57 | tcp::resolver resolver(ioc); |
58 | websocket::stream< | |
59 | beast::ssl_stream<beast::tcp_stream>> ws(ioc, ctx); | |
b32b8144 FG |
60 | |
61 | // Look up the domain name | |
62 | auto const results = resolver.async_resolve(host, port, yield[ec]); | |
63 | if(ec) | |
64 | return fail(ec, "resolve"); | |
65 | ||
92f5a8d4 TL |
66 | // Set a timeout on the operation |
67 | beast::get_lowest_layer(ws).expires_after(std::chrono::seconds(30)); | |
68 | ||
b32b8144 | 69 | // Make the connection on the IP address we get from a lookup |
92f5a8d4 | 70 | beast::get_lowest_layer(ws).async_connect(results, yield[ec]); |
b32b8144 FG |
71 | if(ec) |
72 | return fail(ec, "connect"); | |
73 | ||
92f5a8d4 TL |
74 | // Set a timeout on the operation |
75 | beast::get_lowest_layer(ws).expires_after(std::chrono::seconds(30)); | |
76 | ||
77 | // Set a decorator to change the User-Agent of the handshake | |
78 | ws.set_option(websocket::stream_base::decorator( | |
79 | [](websocket::request_type& req) | |
80 | { | |
81 | req.set(http::field::user_agent, | |
82 | std::string(BOOST_BEAST_VERSION_STRING) + | |
83 | " websocket-client-coro"); | |
84 | })); | |
85 | ||
b32b8144 FG |
86 | // Perform the SSL handshake |
87 | ws.next_layer().async_handshake(ssl::stream_base::client, yield[ec]); | |
88 | if(ec) | |
89 | return fail(ec, "ssl_handshake"); | |
90 | ||
92f5a8d4 TL |
91 | // Turn off the timeout on the tcp_stream, because |
92 | // the websocket stream has its own timeout system. | |
93 | beast::get_lowest_layer(ws).expires_never(); | |
94 | ||
95 | // Set suggested timeout settings for the websocket | |
96 | ws.set_option( | |
97 | websocket::stream_base::timeout::suggested( | |
98 | beast::role_type::client)); | |
99 | ||
b32b8144 FG |
100 | // Perform the websocket handshake |
101 | ws.async_handshake(host, "/", yield[ec]); | |
102 | if(ec) | |
103 | return fail(ec, "handshake"); | |
104 | ||
105 | // Send the message | |
92f5a8d4 | 106 | ws.async_write(net::buffer(std::string(text)), yield[ec]); |
b32b8144 FG |
107 | if(ec) |
108 | return fail(ec, "write"); | |
109 | ||
110 | // This buffer will hold the incoming message | |
92f5a8d4 | 111 | beast::flat_buffer buffer; |
b32b8144 FG |
112 | |
113 | // Read a message into our buffer | |
92f5a8d4 | 114 | ws.async_read(buffer, yield[ec]); |
b32b8144 FG |
115 | if(ec) |
116 | return fail(ec, "read"); | |
117 | ||
118 | // Close the WebSocket connection | |
119 | ws.async_close(websocket::close_code::normal, yield[ec]); | |
120 | if(ec) | |
121 | return fail(ec, "close"); | |
122 | ||
123 | // If we get here then the connection is closed gracefully | |
124 | ||
92f5a8d4 TL |
125 | // The make_printable() function helps print a ConstBufferSequence |
126 | std::cout << beast::make_printable(buffer.data()) << std::endl; | |
b32b8144 FG |
127 | } |
128 | ||
129 | //------------------------------------------------------------------------------ | |
130 | ||
131 | int main(int argc, char** argv) | |
132 | { | |
133 | // Check command line arguments. | |
134 | if(argc != 4) | |
135 | { | |
136 | std::cerr << | |
137 | "Usage: websocket-client-coro-ssl <host> <port> <text>\n" << | |
138 | "Example:\n" << | |
139 | " websocket-client-coro-ssl echo.websocket.org 443 \"Hello, world!\"\n"; | |
140 | return EXIT_FAILURE; | |
141 | } | |
142 | auto const host = argv[1]; | |
143 | auto const port = argv[2]; | |
144 | auto const text = argv[3]; | |
145 | ||
146 | // The io_context is required for all I/O | |
92f5a8d4 | 147 | net::io_context ioc; |
b32b8144 FG |
148 | |
149 | // The SSL context is required, and holds certificates | |
92f5a8d4 | 150 | ssl::context ctx{ssl::context::tlsv12_client}; |
b32b8144 FG |
151 | |
152 | // This holds the root certificate used for verification | |
153 | load_root_certificates(ctx); | |
154 | ||
155 | // Launch the asynchronous operation | |
156 | boost::asio::spawn(ioc, std::bind( | |
157 | &do_session, | |
158 | std::string(host), | |
159 | std::string(port), | |
160 | std::string(text), | |
161 | std::ref(ioc), | |
162 | std::ref(ctx), | |
163 | std::placeholders::_1)); | |
164 | ||
165 | // Run the I/O service. The call will return when | |
11fdf7f2 | 166 | // the socket is closed. |
b32b8144 FG |
167 | ioc.run(); |
168 | ||
169 | return EXIT_SUCCESS; | |
170 | } |