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, synchronous
14 //------------------------------------------------------------------------------
16 #include <boost/beast/core.hpp>
17 #include <boost/beast/websocket.hpp>
18 #include <boost/asio/ip/tcp.hpp>
25 namespace beast
= boost::beast
; // from <boost/beast.hpp>
26 namespace http
= beast::http
; // from <boost/beast/http.hpp>
27 namespace websocket
= beast::websocket
; // from <boost/beast/websocket.hpp>
28 namespace net
= boost::asio
; // from <boost/asio.hpp>
29 using tcp
= boost::asio::ip::tcp
; // from <boost/asio/ip/tcp.hpp>
31 //------------------------------------------------------------------------------
33 // Echoes back all received WebSocket messages
35 do_session(tcp::socket
& socket
)
39 // Construct the stream by moving in the socket
40 websocket::stream
<tcp::socket
> ws
{std::move(socket
)};
42 // Set a decorator to change the Server of the handshake
43 ws
.set_option(websocket::stream_base::decorator(
44 [](websocket::response_type
& res
)
46 res
.set(http::field::server
,
47 std::string(BOOST_BEAST_VERSION_STRING
) +
48 " websocket-server-sync");
51 // Accept the websocket handshake
56 // This buffer will hold the incoming message
57 beast::flat_buffer buffer
;
62 // Echo the message back
63 ws
.text(ws
.got_text());
64 ws
.write(buffer
.data());
67 catch(beast::system_error
const& se
)
69 // This indicates that the session was closed
70 if(se
.code() != websocket::error::closed
)
71 std::cerr
<< "Error: " << se
.code().message() << std::endl
;
73 catch(std::exception
const& e
)
75 std::cerr
<< "Error: " << e
.what() << std::endl
;
79 //------------------------------------------------------------------------------
81 int main(int argc
, char* argv
[])
85 // Check command line arguments.
89 "Usage: websocket-server-sync <address> <port>\n" <<
91 " websocket-server-sync 0.0.0.0 8080\n";
94 auto const address
= net::ip::make_address(argv
[1]);
95 auto const port
= static_cast<unsigned short>(std::atoi(argv
[2]));
97 // The io_context is required for all I/O
98 net::io_context ioc
{1};
100 // The acceptor receives incoming connections
101 tcp::acceptor acceptor
{ioc
, {address
, port
}};
104 // This will receive the new connection
105 tcp::socket socket
{ioc
};
107 // Block until we get a connection
108 acceptor
.accept(socket
);
110 // Launch the session, transferring ownership of the socket
111 std::thread
{std::bind(
113 std::move(socket
))}.detach();
116 catch (const std::exception
& e
)
118 std::cerr
<< "Error: " << e
.what() << std::endl
;