• 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 client, coroutine
13 //
14 //------------------------------------------------------------------------------
15 
16 #include <boost/beast/core.hpp>
17 #include <boost/beast/websocket.hpp>
18 #include <boost/asio/spawn.hpp>
19 #include <cstdlib>
20 #include <functional>
21 #include <iostream>
22 #include <string>
23 
24 namespace beast = boost::beast;         // from <boost/beast.hpp>
25 namespace http = beast::http;           // from <boost/beast/http.hpp>
26 namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp>
27 namespace net = boost::asio;            // from <boost/asio.hpp>
28 using tcp = boost::asio::ip::tcp;       // from <boost/asio/ip/tcp.hpp>
29 
30 //------------------------------------------------------------------------------
31 
32 // Report a failure
33 void
fail(beast::error_code ec,char const * what)34 fail(beast::error_code ec, char const* what)
35 {
36     std::cerr << what << ": " << ec.message() << "\n";
37 }
38 
39 // Sends a WebSocket message and prints the response
40 void
do_session(std::string host,std::string const & port,std::string const & text,net::io_context & ioc,net::yield_context yield)41 do_session(
42     std::string host,
43     std::string const& port,
44     std::string const& text,
45     net::io_context& ioc,
46     net::yield_context yield)
47 {
48     beast::error_code ec;
49 
50     // These objects perform our I/O
51     tcp::resolver resolver(ioc);
52     websocket::stream<beast::tcp_stream> ws(ioc);
53 
54     // Look up the domain name
55     auto const results = resolver.async_resolve(host, port, yield[ec]);
56     if(ec)
57         return fail(ec, "resolve");
58 
59     // Set a timeout on the operation
60     beast::get_lowest_layer(ws).expires_after(std::chrono::seconds(30));
61 
62     // Make the connection on the IP address we get from a lookup
63     auto ep = beast::get_lowest_layer(ws).async_connect(results, yield[ec]);
64     if(ec)
65         return fail(ec, "connect");
66 
67     // Update the host_ string. This will provide the value of the
68     // Host HTTP header during the WebSocket handshake.
69     // See https://tools.ietf.org/html/rfc7230#section-5.4
70     host += ':' + std::to_string(ep.port());
71 
72     // Turn off the timeout on the tcp_stream, because
73     // the websocket stream has its own timeout system.
74     beast::get_lowest_layer(ws).expires_never();
75 
76     // Set suggested timeout settings for the websocket
77     ws.set_option(
78         websocket::stream_base::timeout::suggested(
79             beast::role_type::client));
80 
81     // Set a decorator to change the User-Agent of the handshake
82     ws.set_option(websocket::stream_base::decorator(
83         [](websocket::request_type& req)
84         {
85             req.set(http::field::user_agent,
86                 std::string(BOOST_BEAST_VERSION_STRING) +
87                     " websocket-client-coro");
88         }));
89 
90     // Perform the websocket handshake
91     ws.async_handshake(host, "/", yield[ec]);
92     if(ec)
93         return fail(ec, "handshake");
94 
95     // Send the message
96     ws.async_write(net::buffer(std::string(text)), yield[ec]);
97     if(ec)
98         return fail(ec, "write");
99 
100     // This buffer will hold the incoming message
101     beast::flat_buffer buffer;
102 
103     // Read a message into our buffer
104     ws.async_read(buffer, yield[ec]);
105     if(ec)
106         return fail(ec, "read");
107 
108     // Close the WebSocket connection
109     ws.async_close(websocket::close_code::normal, yield[ec]);
110     if(ec)
111         return fail(ec, "close");
112 
113     // If we get here then the connection is closed gracefully
114 
115     // The make_printable() function helps print a ConstBufferSequence
116     std::cout << beast::make_printable(buffer.data()) << std::endl;
117 }
118 
119 //------------------------------------------------------------------------------
120 
main(int argc,char ** argv)121 int main(int argc, char** argv)
122 {
123     // Check command line arguments.
124     if(argc != 4)
125     {
126         std::cerr <<
127             "Usage: websocket-client-coro <host> <port> <text>\n" <<
128             "Example:\n" <<
129             "    websocket-client-coro echo.websocket.org 80 \"Hello, world!\"\n";
130         return EXIT_FAILURE;
131     }
132     auto const host = argv[1];
133     auto const port = argv[2];
134     auto const text = argv[3];
135 
136     // The io_context is required for all I/O
137     net::io_context ioc;
138 
139     // Launch the asynchronous operation
140     boost::asio::spawn(ioc, std::bind(
141         &do_session,
142         std::string(host),
143         std::string(port),
144         std::string(text),
145         std::ref(ioc),
146         std::placeholders::_1));
147 
148     // Run the I/O service. The call will return when
149     // the socket is closed.
150     ioc.run();
151 
152     return EXIT_SUCCESS;
153 }
154