forked from boostorg/beast
Refactor all examples:
fix #575, fix #604, fix #608, fix #634, fix #712 All examples are rewritten: * Using Best Practices * Mostly self-contained * New examples to complete the feature matrix * The server-framework example is removed
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
#
|
||||
# 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_INCLUDES}
|
||||
Jamfile
|
||||
websocket_client_sync.cpp
|
||||
)
|
||||
@@ -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
|
||||
;
|
||||
@@ -0,0 +1,88 @@
|
||||
//
|
||||
// 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
|
||||
//
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
//
|
||||
// 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>
|
||||
|
||||
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
|
||||
namespace websocket = boost::beast::websocket; // from <boost/beast/websocket.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_service is required for all I/O
|
||||
boost::asio::io_service ios;
|
||||
|
||||
// These objects perform our I/O
|
||||
tcp::resolver resolver{ios};
|
||||
websocket::stream<tcp::socket> ws{ios};
|
||||
|
||||
// Look up the domain name
|
||||
auto const lookup = resolver.resolve({host, port});
|
||||
|
||||
// Make the connection on the IP address we get from a lookup
|
||||
boost::asio::connect(ws.next_layer(), lookup);
|
||||
|
||||
// Perform the websocket handshake
|
||||
ws.handshake(host, "/");
|
||||
|
||||
// Send the message
|
||||
ws.write(boost::asio::buffer(std::string(text)));
|
||||
|
||||
// This buffer will hold the incoming message
|
||||
boost::beast::multi_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 buffers() function helps print a ConstBufferSequence
|
||||
std::cout << boost::beast::buffers(buffer.data()) << std::endl;
|
||||
}
|
||||
catch(std::exception const& e)
|
||||
{
|
||||
std::cerr << "Error: " << e.what() << std::endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
//]
|
||||
Reference in New Issue
Block a user