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