1 //
2 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
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, synchronous
13 //
14 //------------------------------------------------------------------------------
15
16 #include "example/common/root_certificates.hpp"
17
18 #include <boost/beast/core.hpp>
19 #include <boost/beast/ssl.hpp>
20 #include <boost/beast/websocket.hpp>
21 #include <boost/beast/websocket/ssl.hpp>
22 #include <boost/asio/connect.hpp>
23 #include <boost/asio/ip/tcp.hpp>
24 #include <boost/asio/ssl/stream.hpp>
25 #include <cstdlib>
26 #include <iostream>
27 #include <string>
28
29 namespace beast = boost::beast; // from <boost/beast.hpp>
30 namespace http = beast::http; // from <boost/beast/http.hpp>
31 namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp>
32 namespace net = boost::asio; // from <boost/asio.hpp>
33 namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp>
34 using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
35
36 // Sends a WebSocket message and prints the response
main(int argc,char ** argv)37 int main(int argc, char** argv)
38 {
39 try
40 {
41 // Check command line arguments.
42 if(argc != 4)
43 {
44 std::cerr <<
45 "Usage: websocket-client-sync-ssl <host> <port> <text>\n" <<
46 "Example:\n" <<
47 " websocket-client-sync-ssl echo.websocket.org 443 \"Hello, world!\"\n";
48 return EXIT_FAILURE;
49 }
50 std::string host = argv[1];
51 auto const port = argv[2];
52 auto const text = argv[3];
53
54 // The io_context is required for all I/O
55 net::io_context ioc;
56
57 // The SSL context is required, and holds certificates
58 ssl::context ctx{ssl::context::tlsv12_client};
59
60 // This holds the root certificate used for verification
61 load_root_certificates(ctx);
62
63 // These objects perform our I/O
64 tcp::resolver resolver{ioc};
65 websocket::stream<beast::ssl_stream<tcp::socket>> ws{ioc, ctx};
66
67 // Look up the domain name
68 auto const results = resolver.resolve(host, port);
69
70 // Make the connection on the IP address we get from a lookup
71 auto ep = net::connect(get_lowest_layer(ws), results);
72
73 // Update the host_ string. This will provide the value of the
74 // Host HTTP header during the WebSocket handshake.
75 // See https://tools.ietf.org/html/rfc7230#section-5.4
76 host += ':' + std::to_string(ep.port());
77
78 // Perform the SSL handshake
79 ws.next_layer().handshake(ssl::stream_base::client);
80
81 // Set a decorator to change the User-Agent of the handshake
82 ws.set_option(websocket::stream_base::decorator(
83 [](websocket::request_type& req)
84 {
85 req.set(http::field::user_agent,
86 std::string(BOOST_BEAST_VERSION_STRING) +
87 " websocket-client-coro");
88 }));
89
90 // Perform the websocket handshake
91 ws.handshake(host, "/");
92
93 // Send the message
94 ws.write(net::buffer(std::string(text)));
95
96 // This buffer will hold the incoming message
97 beast::flat_buffer buffer;
98
99 // Read a message into our buffer
100 ws.read(buffer);
101
102 // Close the WebSocket connection
103 ws.close(websocket::close_code::normal);
104
105 // If we get here then the connection is closed gracefully
106
107 // The make_printable() function helps print a ConstBufferSequence
108 std::cout << beast::make_printable(buffer.data()) << std::endl;
109 }
110 catch(std::exception const& e)
111 {
112 std::cerr << "Error: " << e.what() << std::endl;
113 return EXIT_FAILURE;
114 }
115 return EXIT_SUCCESS;
116 }
117