]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/beast/example/websocket/server/sync-ssl/websocket_server_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 server, synchronous
14 //------------------------------------------------------------------------------
16 #include "example/common/server_certificate.hpp"
18 #include <boost/beast/core.hpp>
19 #include <boost/beast/websocket.hpp>
20 #include <boost/beast/websocket/ssl.hpp>
21 #include <boost/asio/ip/tcp.hpp>
22 #include <boost/asio/ssl/stream.hpp>
29 using tcp
= boost::asio::ip::tcp
; // from <boost/asio/ip/tcp.hpp>
30 namespace ssl
= boost::asio::ssl
; // from <boost/asio/ssl.hpp>
31 namespace websocket
= boost::beast::websocket
; // from <boost/beast/websocket.hpp>
33 //------------------------------------------------------------------------------
35 // Echoes back all received WebSocket messages
37 do_session(tcp::socket
& socket
, ssl::context
& ctx
)
41 // Construct the websocket stream around the socket
42 websocket::stream
<ssl::stream
<tcp::socket
&>> ws
{socket
, ctx
};
44 // Perform the SSL handshake
45 ws
.next_layer().handshake(ssl::stream_base::server
);
47 // Accept the websocket handshake
52 // This buffer will hold the incoming message
53 boost::beast::multi_buffer buffer
;
58 // Echo the message back
59 ws
.text(ws
.got_text());
60 ws
.write(buffer
.data());
63 catch(boost::system::system_error
const& se
)
65 // This indicates that the session was closed
66 if(se
.code() != websocket::error::closed
)
67 std::cerr
<< "Error: " << se
.code().message() << std::endl
;
69 catch(std::exception
const& e
)
71 std::cerr
<< "Error: " << e
.what() << std::endl
;
75 //------------------------------------------------------------------------------
77 int main(int argc
, char* argv
[])
81 // Check command line arguments.
85 "Usage: websocket-server-sync-ssl <address> <port>\n" <<
87 " websocket-server-sync-ssl 0.0.0.0 8080\n";
90 auto const address
= boost::asio::ip::make_address(argv
[1]);
91 auto const port
= static_cast<unsigned short>(std::atoi(argv
[2]));
93 // The io_context is required for all I/O
94 boost::asio::io_context ioc
{1};
96 // The SSL context is required, and holds certificates
97 ssl::context ctx
{ssl::context::sslv23
};
99 // This holds the self-signed certificate used by the server
100 load_server_certificate(ctx
);
102 // The acceptor receives incoming connections
103 tcp::acceptor acceptor
{ioc
, {address
, port
}};
106 // This will receive the new connection
107 tcp::socket socket
{ioc
};
109 // Block until we get a connection
110 acceptor
.accept(socket
);
112 // Launch the session, transferring ownership of the socket
113 std::thread
{std::bind(
116 std::ref(ctx
))}.detach();
119 catch (const std::exception
& e
)
121 std::cerr
<< "Error: " << e
.what() << std::endl
;