]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/beast/example/websocket/client/async/websocket_client_async.cpp
2 // Copyright (c) 2016-2017 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, asynchronous
14 //------------------------------------------------------------------------------
16 #include <boost/beast/core.hpp>
17 #include <boost/beast/websocket.hpp>
18 #include <boost/asio/connect.hpp>
19 #include <boost/asio/ip/tcp.hpp>
26 using tcp
= boost::asio::ip::tcp
; // from <boost/asio/ip/tcp.hpp>
27 namespace websocket
= boost::beast::websocket
; // from <boost/beast/websocket.hpp>
29 //------------------------------------------------------------------------------
33 fail(boost::system::error_code ec
, char const* what
)
35 std::cerr
<< what
<< ": " << ec
.message() << "\n";
38 // Sends a WebSocket message and prints the response
39 class session
: public std::enable_shared_from_this
<session
>
41 tcp::resolver resolver_
;
42 websocket::stream
<tcp::socket
> ws_
;
43 boost::beast::multi_buffer buffer_
;
48 // Resolver and socket require an io_context
50 session(boost::asio::io_context
& ioc
)
56 // Start the asynchronous operation
63 // Save these for later
67 // Look up the domain name
68 resolver_
.async_resolve(
74 std::placeholders::_1
,
75 std::placeholders::_2
));
80 boost::system::error_code ec
,
81 tcp::resolver::results_type results
)
84 return fail(ec
, "resolve");
86 // Make the connection on the IP address we get from a lookup
87 boost::asio::async_connect(
94 std::placeholders::_1
));
98 on_connect(boost::system::error_code ec
)
101 return fail(ec
, "connect");
103 // Perform the websocket handshake
104 ws_
.async_handshake(host_
, "/",
106 &session::on_handshake
,
108 std::placeholders::_1
));
112 on_handshake(boost::system::error_code ec
)
115 return fail(ec
, "handshake");
119 boost::asio::buffer(text_
),
123 std::placeholders::_1
,
124 std::placeholders::_2
));
129 boost::system::error_code ec
,
130 std::size_t bytes_transferred
)
132 boost::ignore_unused(bytes_transferred
);
135 return fail(ec
, "write");
137 // Read a message into our buffer
143 std::placeholders::_1
,
144 std::placeholders::_2
));
149 boost::system::error_code ec
,
150 std::size_t bytes_transferred
)
152 boost::ignore_unused(bytes_transferred
);
155 return fail(ec
, "read");
157 // Close the WebSocket connection
158 ws_
.async_close(websocket::close_code::normal
,
162 std::placeholders::_1
));
166 on_close(boost::system::error_code ec
)
169 return fail(ec
, "close");
171 // If we get here then the connection is closed gracefully
173 // The buffers() function helps print a ConstBufferSequence
174 std::cout
<< boost::beast::buffers(buffer_
.data()) << std::endl
;
178 //------------------------------------------------------------------------------
180 int main(int argc
, char** argv
)
182 // Check command line arguments.
186 "Usage: websocket-client-async <host> <port> <text>\n" <<
188 " websocket-client-async echo.websocket.org 80 \"Hello, world!\"\n";
191 auto const host
= argv
[1];
192 auto const port
= argv
[2];
193 auto const text
= argv
[3];
195 // The io_context is required for all I/O
196 boost::asio::io_context ioc
;
198 // Launch the asynchronous operation
199 std::make_shared
<session
>(ioc
)->run(host
, port
, text
);
201 // Run the I/O service. The call will return when
202 // the get operation is complete.