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:
Vinnie Falco
2017-08-07 20:53:11 -07:00
parent 20a8f7d75b
commit dd71b0f94d
187 changed files with 11759 additions and 7098 deletions
+18
View File
@@ -0,0 +1,18 @@
#
# Copyright (c) 2013-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
#
add_subdirectory (async)
add_subdirectory (coro)
add_subdirectory (sync)
if (OPENSSL_FOUND)
add_subdirectory (async-ssl)
add_subdirectory (coro-ssl)
add_subdirectory (sync-ssl)
endif()
+17
View File
@@ -0,0 +1,17 @@
#
# Copyright (c) 2013-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
#
build-project async ;
build-project coro ;
build-project sync ;
# VFALCO How do I make this work on Windows and if OpenSSL is not available?
#build-project async-ssl ;
#build-project coro-ssl ;
#build-project sync-ssl ;
@@ -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/common common)
GroupSources(example/websocket/client/async-ssl "/")
add_executable (websocket-client-async-ssl
${BOOST_BEAST_INCLUDES}
${PROJECT_SOURCE_DIR}/example/common/root_certificates.hpp
Jamfile
websocket_client_async_ssl.cpp
)
target_link_libraries (websocket-client-async-ssl
${OPENSSL_LIBRARIES}
)
@@ -0,0 +1,21 @@
#
# 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
#
project
: requirements
<library>ssl
<library>crypto
;
exe websocket-client-async-ssl :
websocket_client_async_ssl.cpp
:
<variant>coverage:<build>no
<variant>ubasan:<build>no
;
@@ -0,0 +1,219 @@
//
// 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 SSL client, asynchronous
//
//------------------------------------------------------------------------------
#include "example/common/root_certificates.hpp"
#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/beast/websocket/ssl.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/ssl/stream.hpp>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <memory>
#include <string>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp>
namespace websocket = boost::beast::websocket; // from <boost/beast/websocket.hpp>
//------------------------------------------------------------------------------
// Report a failure
void
fail(boost::system::error_code ec, char const* what)
{
std::cerr << what << ": " << ec.message() << "\n";
}
// Sends a WebSocket message and prints the response
class session : public std::enable_shared_from_this<session>
{
tcp::resolver resolver_;
websocket::stream<ssl::stream<tcp::socket>> ws_;
boost::beast::multi_buffer buffer_;
std::string host_;
std::string text_;
public:
// Resolver and socket require an io_service
explicit
session(boost::asio::io_service& ios, ssl::context& ctx)
: resolver_(ios)
, ws_(ios, ctx)
{
}
// Start the asynchronous operation
void
run(
char const* host,
char const* port,
char const* text)
{
// Save these for later
host_ = host;
text_ = text;
// Look up the domain name
resolver_.async_resolve({host, port},
std::bind(
&session::on_resolve,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2));
}
void
on_resolve(
boost::system::error_code ec,
tcp::resolver::iterator result)
{
if(ec)
return fail(ec, "resolve");
// Make the connection on the IP address we get from a lookup
boost::asio::async_connect(
ws_.next_layer().next_layer(),
result,
std::bind(
&session::on_connect,
shared_from_this(),
std::placeholders::_1));
}
void
on_connect(boost::system::error_code ec)
{
if(ec)
return fail(ec, "connect");
// Perform the SSL handshake
ws_.next_layer().async_handshake(
ssl::stream_base::client,
std::bind(
&session::on_ssl_handshake,
shared_from_this(),
std::placeholders::_1));
}
void
on_ssl_handshake(boost::system::error_code ec)
{
if(ec)
return fail(ec, "ssl_handshake");
// Perform the websocket handshake
ws_.async_handshake(host_, "/",
std::bind(
&session::on_handshake,
shared_from_this(),
std::placeholders::_1));
}
void
on_handshake(boost::system::error_code ec)
{
if(ec)
return fail(ec, "handshake");
// Send the message
ws_.async_write(
boost::asio::buffer(text_),
std::bind(
&session::on_write,
shared_from_this(),
std::placeholders::_1));
}
void
on_write(boost::system::error_code ec)
{
if(ec)
return fail(ec, "write");
// Read a message into our buffer
ws_.async_read(
buffer_,
std::bind(
&session::on_read,
shared_from_this(),
std::placeholders::_1));
}
void
on_read(boost::system::error_code ec)
{
if(ec)
return fail(ec, "read");
// Close the WebSocket connection
ws_.async_close(websocket::close_code::normal,
std::bind(
&session::on_close,
shared_from_this(),
std::placeholders::_1));
}
void
on_close(boost::system::error_code ec)
{
if(ec)
return fail(ec, "close");
// 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;
}
};
//------------------------------------------------------------------------------
int main(int argc, char** argv)
{
// Check command line arguments.
if(argc != 4)
{
std::cerr <<
"Usage: websocket-client-async-ssl <host> <port> <text>\n" <<
"Example:\n" <<
" websocket-client-async-ssl echo.websocket.org 443 \"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;
// The SSL context is required, and holds certificates
ssl::context ctx{ssl::context::sslv23_client};
// This holds the root certificate used for verification
load_root_certificates(ctx);
// Launch the asynchronous operation
std::make_shared<session>(ios, ctx)->run(host, port, text);
// Run the I/O service. The call will return when
// the get operation is complete.
ios.run();
return EXIT_SUCCESS;
}
@@ -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/async "/")
add_executable (websocket-client-async
${BOOST_BEAST_INCLUDES}
Jamfile
websocket_client_async.cpp
)
+15
View File
@@ -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-async :
websocket_client_async.cpp
:
<variant>coverage:<build>no
<variant>ubasan:<build>no
;
@@ -0,0 +1,193 @@
//
// 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, asynchronous
//
//------------------------------------------------------------------------------
#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <memory>
#include <string>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
namespace websocket = boost::beast::websocket; // from <boost/beast/websocket.hpp>
//------------------------------------------------------------------------------
// Report a failure
void
fail(boost::system::error_code ec, char const* what)
{
std::cerr << what << ": " << ec.message() << "\n";
}
// Sends a WebSocket message and prints the response
class session : public std::enable_shared_from_this<session>
{
tcp::resolver resolver_;
websocket::stream<tcp::socket> ws_;
boost::beast::multi_buffer buffer_;
std::string host_;
std::string text_;
public:
// Resolver and socket require an io_service
explicit
session(boost::asio::io_service& ios)
: resolver_(ios)
, ws_(ios)
{
}
// Start the asynchronous operation
void
run(
char const* host,
char const* port,
char const* text)
{
// Save these for later
host_ = host;
text_ = text;
// Look up the domain name
resolver_.async_resolve({host, port},
std::bind(
&session::on_resolve,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2));
}
void
on_resolve(
boost::system::error_code ec,
tcp::resolver::iterator result)
{
if(ec)
return fail(ec, "resolve");
// Make the connection on the IP address we get from a lookup
boost::asio::async_connect(
ws_.next_layer(),
result,
std::bind(
&session::on_connect,
shared_from_this(),
std::placeholders::_1));
}
void
on_connect(boost::system::error_code ec)
{
if(ec)
return fail(ec, "connect");
// Perform the websocket handshake
ws_.async_handshake(host_, "/",
std::bind(
&session::on_handshake,
shared_from_this(),
std::placeholders::_1));
}
void
on_handshake(boost::system::error_code ec)
{
if(ec)
return fail(ec, "handshake");
// Send the message
ws_.async_write(
boost::asio::buffer(text_),
std::bind(
&session::on_write,
shared_from_this(),
std::placeholders::_1));
}
void
on_write(boost::system::error_code ec)
{
if(ec)
return fail(ec, "write");
// Read a message into our buffer
ws_.async_read(
buffer_,
std::bind(
&session::on_read,
shared_from_this(),
std::placeholders::_1));
}
void
on_read(boost::system::error_code ec)
{
if(ec)
return fail(ec, "read");
// Close the WebSocket connection
ws_.async_close(websocket::close_code::normal,
std::bind(
&session::on_close,
shared_from_this(),
std::placeholders::_1));
}
void
on_close(boost::system::error_code ec)
{
if(ec)
return fail(ec, "close");
// 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;
}
};
//------------------------------------------------------------------------------
int main(int argc, char** argv)
{
// Check command line arguments.
if(argc != 4)
{
std::cerr <<
"Usage: websocket-client-async <host> <port> <text>\n" <<
"Example:\n" <<
" websocket-client-async 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;
// Launch the asynchronous operation
std::make_shared<session>(ios)->run(host, port, text);
// Run the I/O service. The call will return when
// the get operation is complete.
ios.run();
return EXIT_SUCCESS;
}
@@ -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/common common)
GroupSources(example/websocket/client/coro-ssl "/")
add_executable (websocket-client-coro-ssl
${BOOST_BEAST_INCLUDES}
${PROJECT_SOURCE_DIR}/example/common/root_certificates.hpp
Jamfile
websocket_client_coro_ssl.cpp
)
target_link_libraries (websocket-client-coro-ssl
${OPENSSL_LIBRARIES}
)
+21
View File
@@ -0,0 +1,21 @@
#
# 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
#
project
: requirements
<library>ssl
<library>crypto
;
exe websocket-client-coro-ssl :
websocket_client_coro_ssl.cpp
:
<variant>coverage:<build>no
<variant>ubasan:<build>no
;
@@ -0,0 +1,144 @@
//
// 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 SSL client, coroutine
//
//------------------------------------------------------------------------------
#include "example/common/root_certificates.hpp"
#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/beast/websocket/ssl.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/ssl/stream.hpp>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <string>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp>
namespace websocket = boost::beast::websocket; // from <boost/beast/websocket.hpp>
//------------------------------------------------------------------------------
// Report a failure
void
fail(boost::system::error_code ec, char const* what)
{
std::cerr << what << ": " << ec.message() << "\n";
}
// Sends a WebSocket message and prints the response
void
do_session(
std::string const& host,
std::string const& port,
std::string const& text,
boost::asio::io_service& ios,
ssl::context& ctx,
boost::asio::yield_context yield)
{
boost::system::error_code ec;
// These objects perform our I/O
tcp::resolver resolver{ios};
websocket::stream<ssl::stream<tcp::socket>> ws{ios, ctx};
// Look up the domain name
auto const lookup = resolver.async_resolve({host, port}, yield[ec]);
if(ec)
return fail(ec, "resolve");
// Make the connection on the IP address we get from a lookup
boost::asio::async_connect(ws.next_layer().next_layer(), lookup, yield[ec]);
if(ec)
return fail(ec, "connect");
// Perform the SSL handshake
ws.next_layer().async_handshake(ssl::stream_base::client, yield[ec]);
if(ec)
return fail(ec, "ssl_handshake");
// Perform the websocket handshake
ws.async_handshake(host, "/", yield[ec]);
if(ec)
return fail(ec, "handshake");
// Send the message
ws.async_write(boost::asio::buffer(std::string(text)), yield[ec]);
if(ec)
return fail(ec, "write");
// This buffer will hold the incoming message
boost::beast::multi_buffer b;
// Read a message into our buffer
ws.async_read(b, yield[ec]);
if(ec)
return fail(ec, "read");
// Close the WebSocket connection
ws.async_close(websocket::close_code::normal, yield[ec]);
if(ec)
return fail(ec, "close");
// If we get here then the connection is closed gracefully
// The buffers() function helps print a ConstBufferSequence
std::cout << boost::beast::buffers(b.data()) << std::endl;
}
//------------------------------------------------------------------------------
int main(int argc, char** argv)
{
// Check command line arguments.
if(argc != 4)
{
std::cerr <<
"Usage: websocket-client-coro-ssl <host> <port> <text>\n" <<
"Example:\n" <<
" websocket-client-coro-ssl echo.websocket.org 443 \"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;
// The SSL context is required, and holds certificates
ssl::context ctx{ssl::context::sslv23_client};
// This holds the root certificate used for verification
load_root_certificates(ctx);
// Launch the asynchronous operation
boost::asio::spawn(ios, std::bind(
&do_session,
std::string(host),
std::string(port),
std::string(text),
std::ref(ios),
std::ref(ctx),
std::placeholders::_1));
// Run the I/O service. The call will return when
// the get operation is complete.
ios.run();
return EXIT_SUCCESS;
}
@@ -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/coro "/")
add_executable (websocket-client-coro
${BOOST_BEAST_INCLUDES}
Jamfile
websocket_client_coro.cpp
)
+15
View File
@@ -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-coro :
websocket_client_coro.cpp
:
<variant>coverage:<build>no
<variant>ubasan:<build>no
;
@@ -0,0 +1,126 @@
//
// 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, coroutine
//
//------------------------------------------------------------------------------
#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <cstdlib>
#include <functional>
#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>
//------------------------------------------------------------------------------
// Report a failure
void
fail(boost::system::error_code ec, char const* what)
{
std::cerr << what << ": " << ec.message() << "\n";
}
// Sends a WebSocket message and prints the response
void
do_session(
std::string const& host,
std::string const& port,
std::string const& text,
boost::asio::io_service& ios,
boost::asio::yield_context yield)
{
boost::system::error_code ec;
// 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.async_resolve({host, port}, yield[ec]);
if(ec)
return fail(ec, "resolve");
// Make the connection on the IP address we get from a lookup
boost::asio::async_connect(ws.next_layer(), lookup, yield[ec]);
if(ec)
return fail(ec, "connect");
// Perform the websocket handshake
ws.async_handshake(host, "/", yield[ec]);
if(ec)
return fail(ec, "handshake");
// Send the message
ws.async_write(boost::asio::buffer(std::string(text)), yield[ec]);
if(ec)
return fail(ec, "write");
// This buffer will hold the incoming message
boost::beast::multi_buffer b;
// Read a message into our buffer
ws.async_read(b, yield[ec]);
if(ec)
return fail(ec, "read");
// Close the WebSocket connection
ws.async_close(websocket::close_code::normal, yield[ec]);
if(ec)
return fail(ec, "close");
// If we get here then the connection is closed gracefully
// The buffers() function helps print a ConstBufferSequence
std::cout << boost::beast::buffers(b.data()) << std::endl;
}
//------------------------------------------------------------------------------
int main(int argc, char** argv)
{
// Check command line arguments.
if(argc != 4)
{
std::cerr <<
"Usage: websocket-client-coro <host> <port> <text>\n" <<
"Example:\n" <<
" websocket-client-coro 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;
// Launch the asynchronous operation
boost::asio::spawn(ios, std::bind(
&do_session,
std::string(host),
std::string(port),
std::string(text),
std::ref(ios),
std::placeholders::_1));
// Run the I/O service. The call will return when
// the get operation is complete.
ios.run();
return EXIT_SUCCESS;
}
@@ -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/common common)
GroupSources(example/websocket/client/sync-ssl "/")
add_executable (websocket-client-sync-ssl
${BOOST_BEAST_INCLUDES}
${PROJECT_SOURCE_DIR}/example/common/root_certificates.hpp
Jamfile
websocket_client_sync_ssl.cpp
)
target_link_libraries (websocket-client-sync-ssl
${OPENSSL_LIBRARIES}
)
+21
View File
@@ -0,0 +1,21 @@
#
# 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
#
project
: requirements
<library>ssl
<library>crypto
;
exe websocket-client-sync-ssl :
websocket_client_sync_ssl.cpp
:
<variant>coverage:<build>no
<variant>ubasan:<build>no
;
@@ -0,0 +1,98 @@
//
// 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 SSL client, synchronous
//
//------------------------------------------------------------------------------
#include "example/common/root_certificates.hpp"
#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/beast/websocket/ssl.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/ssl/stream.hpp>
#include <cstdlib>
#include <iostream>
#include <string>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.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-ssl <host> <port> <text>\n" <<
"Example:\n" <<
" websocket-client-sync-ssl echo.websocket.org 443 \"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;
// The SSL context is required, and holds certificates
ssl::context ctx{ssl::context::sslv23_client};
// This holds the root certificate used for verification
load_root_certificates(ctx);
// These objects perform our I/O
tcp::resolver resolver{ios};
websocket::stream<ssl::stream<tcp::socket>> ws{ios, ctx};
// 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().next_layer(), lookup);
// Perform the SSL handshake
ws.next_layer().handshake(ssl::stream_base::client);
// 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 b;
// Read a message into our buffer
ws.read(b);
// 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(b.data()) << std::endl;
}
catch(std::exception const& e)
{
std::cerr << "Error: " << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
@@ -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
)
+15
View File
@@ -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;
}
//]