• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (c) 2016-2019 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 // Official repository: https://github.com/boostorg/beast
8 //
9 
10 //------------------------------------------------------------------------------
11 //
12 // Example: WebSocket server, synchronous
13 //
14 //------------------------------------------------------------------------------
15 
16 #include <boost/beast/core.hpp>
17 #include <boost/beast/websocket.hpp>
18 #include <boost/asio/ip/tcp.hpp>
19 #include <cstdlib>
20 #include <functional>
21 #include <iostream>
22 #include <string>
23 #include <thread>
24 
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>
30 
31 //------------------------------------------------------------------------------
32 
33 // Echoes back all received WebSocket messages
34 void
do_session(tcp::socket socket)35 do_session(tcp::socket socket)
36 {
37     try
38     {
39         // Construct the stream by moving in the socket
40         websocket::stream<tcp::socket> ws{std::move(socket)};
41 
42         // Set a decorator to change the Server of the handshake
43         ws.set_option(websocket::stream_base::decorator(
44             [](websocket::response_type& res)
45             {
46                 res.set(http::field::server,
47                     std::string(BOOST_BEAST_VERSION_STRING) +
48                         " websocket-server-sync");
49             }));
50 
51         // Accept the websocket handshake
52         ws.accept();
53 
54         for(;;)
55         {
56             // This buffer will hold the incoming message
57             beast::flat_buffer buffer;
58 
59             // Read a message
60             ws.read(buffer);
61 
62             // Echo the message back
63             ws.text(ws.got_text());
64             ws.write(buffer.data());
65         }
66     }
67     catch(beast::system_error const& se)
68     {
69         // This indicates that the session was closed
70         if(se.code() != websocket::error::closed)
71             std::cerr << "Error: " << se.code().message() << std::endl;
72     }
73     catch(std::exception const& e)
74     {
75         std::cerr << "Error: " << e.what() << std::endl;
76     }
77 }
78 
79 //------------------------------------------------------------------------------
80 
main(int argc,char * argv[])81 int main(int argc, char* argv[])
82 {
83     try
84     {
85         // Check command line arguments.
86         if (argc != 3)
87         {
88             std::cerr <<
89                 "Usage: websocket-server-sync <address> <port>\n" <<
90                 "Example:\n" <<
91                 "    websocket-server-sync 0.0.0.0 8080\n";
92             return EXIT_FAILURE;
93         }
94         auto const address = net::ip::make_address(argv[1]);
95         auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
96 
97         // The io_context is required for all I/O
98         net::io_context ioc{1};
99 
100         // The acceptor receives incoming connections
101         tcp::acceptor acceptor{ioc, {address, port}};
102         for(;;)
103         {
104             // This will receive the new connection
105             tcp::socket socket{ioc};
106 
107             // Block until we get a connection
108             acceptor.accept(socket);
109 
110             // Launch the session, transferring ownership of the socket
111             std::thread(
112                 &do_session,
113                 std::move(socket)).detach();
114         }
115     }
116     catch (const std::exception& e)
117     {
118         std::cerr << "Error: " << e.what() << std::endl;
119         return EXIT_FAILURE;
120     }
121 }
122