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 client, synchronous
14 //------------------------------------------------------------------------------
16 //[example_websocket_client
18 #include <boost/beast/core.hpp>
19 #include <boost/beast/websocket.hpp>
20 #include <boost/asio/connect.hpp>
21 #include <boost/asio/ip/tcp.hpp>
26 namespace beast
= boost::beast
; // from <boost/beast.hpp>
27 namespace http
= beast::http
; // from <boost/beast/http.hpp>
28 namespace websocket
= beast::websocket
; // from <boost/beast/websocket.hpp>
29 namespace net
= boost::asio
; // from <boost/asio.hpp>
30 using tcp
= boost::asio::ip::tcp
; // from <boost/asio/ip/tcp.hpp>
32 // Sends a WebSocket message and prints the response
33 int main(int argc
, char** argv
)
37 // Check command line arguments.
41 "Usage: websocket-client-sync <host> <port> <text>\n" <<
43 " websocket-client-sync echo.websocket.org 80 \"Hello, world!\"\n";
46 std::string host
= argv
[1];
47 auto const port
= argv
[2];
48 auto const text
= argv
[3];
50 // The io_context is required for all I/O
53 // These objects perform our I/O
54 tcp::resolver resolver
{ioc
};
55 websocket::stream
<tcp::socket
> ws
{ioc
};
57 // Look up the domain name
58 auto const results
= resolver
.resolve(host
, port
);
60 // Make the connection on the IP address we get from a lookup
61 auto ep
= net::connect(ws
.next_layer(), results
);
63 // Update the host_ string. This will provide the value of the
64 // Host HTTP header during the WebSocket handshake.
65 // See https://tools.ietf.org/html/rfc7230#section-5.4
66 host
+= ':' + std::to_string(ep
.port());
68 // Set a decorator to change the User-Agent of the handshake
69 ws
.set_option(websocket::stream_base::decorator(
70 [](websocket::request_type
& req
)
72 req
.set(http::field::user_agent
,
73 std::string(BOOST_BEAST_VERSION_STRING
) +
74 " websocket-client-coro");
77 // Perform the websocket handshake
78 ws
.handshake(host
, "/");
81 ws
.write(net::buffer(std::string(text
)));
83 // This buffer will hold the incoming message
84 beast::flat_buffer buffer
;
86 // Read a message into our buffer
89 // Close the WebSocket connection
90 ws
.close(websocket::close_code::normal
);
92 // If we get here then the connection is closed gracefully
94 // The make_printable() function helps print a ConstBufferSequence
95 std::cout
<< beast::make_printable(buffer
.data()) << std::endl
;
97 catch(std::exception
const& e
)
99 std::cerr
<< "Error: " << e
.what() << std::endl
;