]> git.proxmox.com Git - ceph.git/blob - ceph/src/Beast/examples/ssl/websocket_ssl_example.cpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / Beast / examples / ssl / websocket_ssl_example.cpp
1 //
2 // Copyright (c) 2013-2017 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
8 #include <beast/core/to_string.hpp>
9 #include <beast/websocket.hpp>
10 #include <beast/websocket/ssl.hpp>
11 #include <boost/asio.hpp>
12 #include <boost/asio/ssl.hpp>
13 #include <iostream>
14 #include <string>
15
16 int main()
17 {
18 using boost::asio::connect;
19 using socket = boost::asio::ip::tcp::socket;
20 using resolver = boost::asio::ip::tcp::resolver;
21 using io_service = boost::asio::io_service;
22 namespace ssl = boost::asio::ssl;
23
24 // Normal boost::asio setup
25 std::string const host = "echo.websocket.org";
26 io_service ios;
27 resolver r{ios};
28 socket sock{ios};
29 connect(sock, r.resolve(resolver::query{host, "https"}));
30
31 // Perform SSL handshaking
32 using stream_type = ssl::stream<socket&>;
33 ssl::context ctx{ssl::context::sslv23};
34 stream_type stream{sock, ctx};
35 stream.set_verify_mode(ssl::verify_none);
36 stream.handshake(ssl::stream_base::client);
37
38 // Secure WebSocket connect and send message using Beast
39 beast::websocket::stream<stream_type&> ws{stream};
40 ws.handshake(host, "/");
41 ws.write(boost::asio::buffer("Hello, world!"));
42
43 // Receive Secure WebSocket message, print and close using Beast
44 beast::streambuf sb;
45 beast::websocket::opcode op;
46 ws.read(op, sb);
47 ws.close(beast::websocket::close_code::normal);
48 std::cout << to_string(sb.data()) << "\n";
49 }