]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/beast/example/websocket/client/sync-ssl/websocket_client_sync_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, synchronous
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>
28 using tcp
= boost::asio::ip::tcp
; // from <boost/asio/ip/tcp.hpp>
29 namespace ssl
= boost::asio::ssl
; // from <boost/asio/ssl.hpp>
30 namespace websocket
= boost::beast::websocket
; // from <boost/beast/websocket.hpp>
32 // Sends a WebSocket message and prints the response
33 int main(int argc
, char** argv
)
37 // Check command line arguments.
41 "Usage: websocket-client-sync-ssl <host> <port> <text>\n" <<
43 " websocket-client-sync-ssl echo.websocket.org 443 \"Hello, world!\"\n";
46 auto const host
= argv
[1];
47 auto const port
= argv
[2];
48 auto const text
= argv
[3];
50 // The io_context is required for all I/O
51 boost::asio::io_context ioc
;
53 // The SSL context is required, and holds certificates
54 ssl::context ctx
{ssl::context::sslv23_client
};
56 // This holds the root certificate used for verification
57 load_root_certificates(ctx
);
59 // These objects perform our I/O
60 tcp::resolver resolver
{ioc
};
61 websocket::stream
<ssl::stream
<tcp::socket
>> ws
{ioc
, ctx
};
63 // Look up the domain name
64 auto const results
= resolver
.resolve(host
, port
);
66 // Make the connection on the IP address we get from a lookup
67 boost::asio::connect(ws
.next_layer().next_layer(), results
.begin(), results
.end());
69 // Perform the SSL handshake
70 ws
.next_layer().handshake(ssl::stream_base::client
);
72 // Perform the websocket handshake
73 ws
.handshake(host
, "/");
76 ws
.write(boost::asio::buffer(std::string(text
)));
78 // This buffer will hold the incoming message
79 boost::beast::multi_buffer b
;
81 // Read a message into our buffer
84 // Close the WebSocket connection
85 ws
.close(websocket::close_code::normal
);
87 // If we get here then the connection is closed gracefully
89 // The buffers() function helps print a ConstBufferSequence
90 std::cout
<< boost::beast::buffers(b
.data()) << std::endl
;
92 catch(std::exception
const& e
)
94 std::cerr
<< "Error: " << e
.what() << std::endl
;