Use tcp_stream in WebSocket client examples

This commit is contained in:
Vinnie Falco
2019-02-09 22:20:33 -08:00
parent cab2472ee9
commit 920909673a
5 changed files with 37 additions and 24 deletions

View File

@ -4,6 +4,7 @@ Version 213:
* basic_stream subsumes stranded_stream: * basic_stream subsumes stranded_stream:
* Use timeouts in HTTP server examples * Use timeouts in HTTP server examples
* Use timeouts in HTTP client examples * Use timeouts in HTTP client examples
* Use tcp_stream in WebSocket client examples
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------

View File

@ -18,9 +18,7 @@
#include <boost/beast/core.hpp> #include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp> #include <boost/beast/websocket.hpp>
#include <boost/beast/websocket/ssl.hpp> #include <boost/beast/websocket/ssl.hpp>
#include <boost/asio/connect.hpp> #include <boost/beast/_experimental/core/ssl_stream.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/ssl/stream.hpp>
#include <cstdlib> #include <cstdlib>
#include <functional> #include <functional>
#include <iostream> #include <iostream>
@ -47,7 +45,8 @@ fail(beast::error_code ec, char const* what)
class session : public std::enable_shared_from_this<session> class session : public std::enable_shared_from_this<session>
{ {
tcp::resolver resolver_; tcp::resolver resolver_;
websocket::stream<ssl::stream<tcp::socket>> ws_; websocket::stream<beast::ssl_stream<
beast::tcp_stream<net::io_context::executor_type>>> ws_;
beast::multi_buffer buffer_; beast::multi_buffer buffer_;
std::string host_; std::string host_;
std::string text_; std::string text_;
@ -91,11 +90,13 @@ public:
if(ec) if(ec)
return fail(ec, "resolve"); return fail(ec, "resolve");
// Set a timeout on the operation
beast::get_lowest_layer(ws_).expires_after(std::chrono::seconds(30));
// Make the connection on the IP address we get from a lookup // Make the connection on the IP address we get from a lookup
net::async_connect( beast::async_connect(
ws_.next_layer().next_layer(), beast::get_lowest_layer(ws_),
results.begin(), results,
results.end(),
std::bind( std::bind(
&session::on_connect, &session::on_connect,
shared_from_this(), shared_from_this(),
@ -108,6 +109,9 @@ public:
if(ec) if(ec)
return fail(ec, "connect"); return fail(ec, "connect");
// Set a timeout on the operation
beast::get_lowest_layer(ws_).expires_after(std::chrono::seconds(30));
// Perform the SSL handshake // Perform the SSL handshake
ws_.next_layer().async_handshake( ws_.next_layer().async_handshake(
ssl::stream_base::client, ssl::stream_base::client,

View File

@ -15,8 +15,6 @@
#include <boost/beast/core.hpp> #include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp> #include <boost/beast/websocket.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <cstdlib> #include <cstdlib>
#include <functional> #include <functional>
#include <iostream> #include <iostream>
@ -42,7 +40,8 @@ fail(beast::error_code ec, char const* what)
class session : public std::enable_shared_from_this<session> class session : public std::enable_shared_from_this<session>
{ {
tcp::resolver resolver_; tcp::resolver resolver_;
websocket::stream<tcp::socket> ws_; websocket::stream<
beast::tcp_stream<net::io_context::executor_type>> ws_;
beast::multi_buffer buffer_; beast::multi_buffer buffer_;
std::string host_; std::string host_;
std::string text_; std::string text_;
@ -86,11 +85,13 @@ public:
if(ec) if(ec)
return fail(ec, "resolve"); return fail(ec, "resolve");
// Set the timeout for the operation
beast::get_lowest_layer(ws_).expires_after(std::chrono::seconds(30));
// Make the connection on the IP address we get from a lookup // Make the connection on the IP address we get from a lookup
net::async_connect( beast::async_connect(
ws_.next_layer(), beast::get_lowest_layer(ws_),
results.begin(), results,
results.end(),
std::bind( std::bind(
&session::on_connect, &session::on_connect,
shared_from_this(), shared_from_this(),

View File

@ -18,10 +18,8 @@
#include <boost/beast/core.hpp> #include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp> #include <boost/beast/websocket.hpp>
#include <boost/beast/websocket/ssl.hpp> #include <boost/beast/websocket/ssl.hpp>
#include <boost/asio/connect.hpp> #include <boost/beast/_experimental/core/ssl_stream.hpp>
#include <boost/asio/spawn.hpp> #include <boost/asio/spawn.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/ssl/stream.hpp>
#include <cstdlib> #include <cstdlib>
#include <functional> #include <functional>
#include <iostream> #include <iostream>
@ -57,18 +55,25 @@ do_session(
// These objects perform our I/O // These objects perform our I/O
tcp::resolver resolver{ioc}; tcp::resolver resolver{ioc};
websocket::stream<ssl::stream<tcp::socket>> ws{ioc, ctx}; websocket::stream<ssl::stream<
beast::tcp_stream<net::io_context::executor_type>>> ws(ioc, ctx);
// Look up the domain name // Look up the domain name
auto const results = resolver.async_resolve(host, port, yield[ec]); auto const results = resolver.async_resolve(host, port, yield[ec]);
if(ec) if(ec)
return fail(ec, "resolve"); return fail(ec, "resolve");
// Set a timeout on the operation
beast::get_lowest_layer(ws).expires_after(std::chrono::seconds(30));
// Make the connection on the IP address we get from a lookup // Make the connection on the IP address we get from a lookup
net::async_connect(ws.next_layer().next_layer(), results.begin(), results.end(), yield[ec]); beast::async_connect(beast::get_lowest_layer(ws), results, yield[ec]);
if(ec) if(ec)
return fail(ec, "connect"); return fail(ec, "connect");
// Set a timeout on the operation
beast::get_lowest_layer(ws).expires_after(std::chrono::seconds(30));
// Perform the SSL handshake // Perform the SSL handshake
ws.next_layer().async_handshake(ssl::stream_base::client, yield[ec]); ws.next_layer().async_handshake(ssl::stream_base::client, yield[ec]);
if(ec) if(ec)

View File

@ -15,9 +15,7 @@
#include <boost/beast/core.hpp> #include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp> #include <boost/beast/websocket.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/spawn.hpp> #include <boost/asio/spawn.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <cstdlib> #include <cstdlib>
#include <functional> #include <functional>
#include <iostream> #include <iostream>
@ -51,15 +49,19 @@ do_session(
// These objects perform our I/O // These objects perform our I/O
tcp::resolver resolver{ioc}; tcp::resolver resolver{ioc};
websocket::stream<tcp::socket> ws{ioc}; websocket::stream<
beast::tcp_stream<net::io_context::executor_type>> ws(ioc);
// Look up the domain name // Look up the domain name
auto const results = resolver.async_resolve(host, port, yield[ec]); auto const results = resolver.async_resolve(host, port, yield[ec]);
if(ec) if(ec)
return fail(ec, "resolve"); return fail(ec, "resolve");
// Set a timeout on the operation
beast::get_lowest_layer(ws).expires_after(std::chrono::seconds(30));
// Make the connection on the IP address we get from a lookup // Make the connection on the IP address we get from a lookup
net::async_connect(ws.next_layer(), results.begin(), results.end(), yield[ec]); beast::async_connect(ws.next_layer(), results, yield[ec]);
if(ec) if(ec)
return fail(ec, "connect"); return fail(ec, "connect");