diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
commit | 483eb2f56657e8e7f419ab1a4fab8dce9ade8609 (patch) | |
tree | e5d88d25d870d5dedacb6bbdbe2a966086a0a5cf /src/boost/libs/beast/example/websocket/client/sync | |
parent | Initial commit. (diff) | |
download | ceph-upstream.tar.xz ceph-upstream.zip |
Adding upstream version 14.2.21.upstream/14.2.21upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/boost/libs/beast/example/websocket/client/sync')
3 files changed, 138 insertions, 0 deletions
diff --git a/src/boost/libs/beast/example/websocket/client/sync/CMakeLists.txt b/src/boost/libs/beast/example/websocket/client/sync/CMakeLists.txt new file mode 100644 index 00000000..d8606ab5 --- /dev/null +++ b/src/boost/libs/beast/example/websocket/client/sync/CMakeLists.txt @@ -0,0 +1,23 @@ +# +# Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com) +# +# Distributed under the Boost Software License, Version 1.0. (See accompanying +# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +# +# Official repository: https://github.com/boostorg/beast +# + +GroupSources(include/boost/beast beast) +GroupSources(example/websocket/client/sync "/") + +add_executable (websocket-client-sync + ${BOOST_BEAST_FILES} + Jamfile + websocket_client_sync.cpp +) + +target_link_libraries(websocket-client-sync + lib-asio + lib-beast) + +set_property(TARGET websocket-client-sync PROPERTY FOLDER "example-websocket-client") diff --git a/src/boost/libs/beast/example/websocket/client/sync/Jamfile b/src/boost/libs/beast/example/websocket/client/sync/Jamfile new file mode 100644 index 00000000..ea71a0ce --- /dev/null +++ b/src/boost/libs/beast/example/websocket/client/sync/Jamfile @@ -0,0 +1,15 @@ +# +# Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com) +# +# Distributed under the Boost Software License, Version 1.0. (See accompanying +# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +# +# Official repository: https://github.com/boostorg/beast +# + +exe websocket-client-sync : + websocket_client_sync.cpp + : + <variant>coverage:<build>no + <variant>ubasan:<build>no + ; diff --git a/src/boost/libs/beast/example/websocket/client/sync/websocket_client_sync.cpp b/src/boost/libs/beast/example/websocket/client/sync/websocket_client_sync.cpp new file mode 100644 index 00000000..c80ae41b --- /dev/null +++ b/src/boost/libs/beast/example/websocket/client/sync/websocket_client_sync.cpp @@ -0,0 +1,100 @@ +// +// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com) +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// Official repository: https://github.com/boostorg/beast +// + +//------------------------------------------------------------------------------ +// +// Example: WebSocket client, synchronous +// +//------------------------------------------------------------------------------ + +//[example_websocket_client + +#include <boost/beast/core.hpp> +#include <boost/beast/websocket.hpp> +#include <boost/asio/connect.hpp> +#include <boost/asio/ip/tcp.hpp> +#include <cstdlib> +#include <iostream> +#include <string> + +namespace beast = boost::beast; // from <boost/beast.hpp> +namespace http = beast::http; // from <boost/beast/http.hpp> +namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp> +namespace net = boost::asio; // from <boost/asio.hpp> +using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp> + +// Sends a WebSocket message and prints the response +int main(int argc, char** argv) +{ + try + { + // Check command line arguments. + if(argc != 4) + { + std::cerr << + "Usage: websocket-client-sync <host> <port> <text>\n" << + "Example:\n" << + " websocket-client-sync echo.websocket.org 80 \"Hello, world!\"\n"; + return EXIT_FAILURE; + } + auto const host = argv[1]; + auto const port = argv[2]; + auto const text = argv[3]; + + // The io_context is required for all I/O + net::io_context ioc; + + // These objects perform our I/O + tcp::resolver resolver{ioc}; + websocket::stream<tcp::socket> ws{ioc}; + + // Look up the domain name + auto const results = resolver.resolve(host, port); + + // Make the connection on the IP address we get from a lookup + net::connect(ws.next_layer(), results.begin(), results.end()); + + // Set a decorator to change the User-Agent of the handshake + ws.set_option(websocket::stream_base::decorator( + [](websocket::request_type& req) + { + req.set(http::field::user_agent, + std::string(BOOST_BEAST_VERSION_STRING) + + " websocket-client-coro"); + })); + + // Perform the websocket handshake + ws.handshake(host, "/"); + + // Send the message + ws.write(net::buffer(std::string(text))); + + // This buffer will hold the incoming message + beast::flat_buffer buffer; + + // Read a message into our buffer + ws.read(buffer); + + // Close the WebSocket connection + ws.close(websocket::close_code::normal); + + // If we get here then the connection is closed gracefully + + // The make_printable() function helps print a ConstBufferSequence + std::cout << beast::make_printable(buffer.data()) << std::endl; + } + catch(std::exception const& e) + { + std::cerr << "Error: " << e.what() << std::endl; + return EXIT_FAILURE; + } + return EXIT_SUCCESS; +} + +//] |