2 // Copyright (c) 2016-2019 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 server, asynchronous
14 //------------------------------------------------------------------------------
16 #include <boost/beast/core.hpp>
17 #include <boost/beast/websocket.hpp>
18 #include <boost/asio/dispatch.hpp>
19 #include <boost/asio/strand.hpp>
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 using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
35 //------------------------------------------------------------------------------
39 fail(beast::error_code ec, char const* what)
41 std::cerr << what << ": " << ec.message() << "\n";
44 // Echoes back all received WebSocket messages
45 class session : public std::enable_shared_from_this<session>
47 websocket::stream<beast::tcp_stream> ws_;
48 beast::flat_buffer buffer_;
51 // Take ownership of the socket
53 session(tcp::socket&& socket)
54 : ws_(std::move(socket))
58 // Get on the correct executor
62 // We need to be executing within a strand to perform async operations
63 // on the I/O objects in this session. Although not strictly necessary
64 // for single-threaded contexts, this example code is written to be
65 // thread-safe by default.
66 net::dispatch(ws_.get_executor(),
67 beast::bind_front_handler(
72 // Start the asynchronous operation
76 // Set suggested timeout settings for the websocket
78 websocket::stream_base::timeout::suggested(
79 beast::role_type::server));
81 // Set a decorator to change the Server of the handshake
82 ws_.set_option(websocket::stream_base::decorator(
83 [](websocket::response_type& res)
85 res.set(http::field::server,
86 std::string(BOOST_BEAST_VERSION_STRING) +
87 " websocket-server-async");
89 // Accept the websocket handshake
91 beast::bind_front_handler(
97 on_accept(beast::error_code ec)
100 return fail(ec, "accept");
109 // Read a message into our buffer
112 beast::bind_front_handler(
114 shared_from_this()));
119 beast::error_code ec,
120 std::size_t bytes_transferred)
122 boost::ignore_unused(bytes_transferred);
124 // This indicates that the session was closed
125 if(ec == websocket::error::closed)
132 ws_.text(ws_.got_text());
135 beast::bind_front_handler(
137 shared_from_this()));
142 beast::error_code ec,
143 std::size_t bytes_transferred)
145 boost::ignore_unused(bytes_transferred);
148 return fail(ec, "write");
151 buffer_.consume(buffer_.size());
158 //------------------------------------------------------------------------------
160 // Accepts incoming connections and launches the sessions
161 class listener : public std::enable_shared_from_this<listener>
163 net::io_context& ioc_;
164 tcp::acceptor acceptor_;
168 net::io_context& ioc,
169 tcp::endpoint endpoint)
173 beast::error_code ec;
176 acceptor_.open(endpoint.protocol(), ec);
183 // Allow address reuse
184 acceptor_.set_option(net::socket_base::reuse_address(true), ec);
187 fail(ec, "set_option");
191 // Bind to the server address
192 acceptor_.bind(endpoint, ec);
199 // Start listening for connections
201 net::socket_base::max_listen_connections, ec);
209 // Start accepting incoming connections
220 // The new connection gets its own strand
221 acceptor_.async_accept(
222 net::make_strand(ioc_),
223 beast::bind_front_handler(
224 &listener::on_accept,
225 shared_from_this()));
229 on_accept(beast::error_code ec, tcp::socket socket)
237 // Create the session and run it
238 std::make_shared<session>(std::move(socket))->run();
241 // Accept another connection
246 //------------------------------------------------------------------------------
248 int main(int argc, char* argv[])
250 // Check command line arguments.
254 "Usage: websocket-server-async <address> <port> <threads>\n" <<
256 " websocket-server-async 0.0.0.0 8080 1\n";
259 auto const address = net::ip::make_address(argv[1]);
260 auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
261 auto const threads = std::max<int>(1, std::atoi(argv[3]));
263 // The io_context is required for all I/O
264 net::io_context ioc{threads};
266 // Create and launch a listening port
267 std::make_shared<listener>(ioc, tcp::endpoint{address, port})->run();
269 // Run the I/O service on the requested number of threads
270 std::vector<std::thread> v;
271 v.reserve(threads - 1);
272 for(auto i = threads - 1; i > 0; --i)