From 920909673ad4a33724b51e8daba5db51c01e17a8 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Sat, 9 Feb 2019 22:20:33 -0800 Subject: [PATCH] Use tcp_stream in WebSocket client examples --- CHANGELOG.md | 1 + .../async-ssl/websocket_client_async_ssl.cpp | 20 +++++++++++-------- .../client/async/websocket_client_async.cpp | 15 +++++++------- .../coro-ssl/websocket_client_coro_ssl.cpp | 15 +++++++++----- .../client/coro/websocket_client_coro.cpp | 10 ++++++---- 5 files changed, 37 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d99dd894..363716a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ Version 213: * basic_stream subsumes stranded_stream: * Use timeouts in HTTP server examples * Use timeouts in HTTP client examples +* Use tcp_stream in WebSocket client examples -------------------------------------------------------------------------------- diff --git a/example/websocket/client/async-ssl/websocket_client_async_ssl.cpp b/example/websocket/client/async-ssl/websocket_client_async_ssl.cpp index 2135a22b..dc387f82 100644 --- a/example/websocket/client/async-ssl/websocket_client_async_ssl.cpp +++ b/example/websocket/client/async-ssl/websocket_client_async_ssl.cpp @@ -18,9 +18,7 @@ #include #include #include -#include -#include -#include +#include #include #include #include @@ -47,7 +45,8 @@ fail(beast::error_code ec, char const* what) class session : public std::enable_shared_from_this { tcp::resolver resolver_; - websocket::stream> ws_; + websocket::stream>> ws_; beast::multi_buffer buffer_; std::string host_; std::string text_; @@ -91,11 +90,13 @@ public: if(ec) 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 - net::async_connect( - ws_.next_layer().next_layer(), - results.begin(), - results.end(), + beast::async_connect( + beast::get_lowest_layer(ws_), + results, std::bind( &session::on_connect, shared_from_this(), @@ -108,6 +109,9 @@ public: if(ec) 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 ws_.next_layer().async_handshake( ssl::stream_base::client, diff --git a/example/websocket/client/async/websocket_client_async.cpp b/example/websocket/client/async/websocket_client_async.cpp index 6d1e40e4..f0ef2018 100644 --- a/example/websocket/client/async/websocket_client_async.cpp +++ b/example/websocket/client/async/websocket_client_async.cpp @@ -15,8 +15,6 @@ #include #include -#include -#include #include #include #include @@ -42,7 +40,8 @@ fail(beast::error_code ec, char const* what) class session : public std::enable_shared_from_this { tcp::resolver resolver_; - websocket::stream ws_; + websocket::stream< + beast::tcp_stream> ws_; beast::multi_buffer buffer_; std::string host_; std::string text_; @@ -86,11 +85,13 @@ public: if(ec) 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 - net::async_connect( - ws_.next_layer(), - results.begin(), - results.end(), + beast::async_connect( + beast::get_lowest_layer(ws_), + results, std::bind( &session::on_connect, shared_from_this(), diff --git a/example/websocket/client/coro-ssl/websocket_client_coro_ssl.cpp b/example/websocket/client/coro-ssl/websocket_client_coro_ssl.cpp index 59e48e4d..c84edb17 100644 --- a/example/websocket/client/coro-ssl/websocket_client_coro_ssl.cpp +++ b/example/websocket/client/coro-ssl/websocket_client_coro_ssl.cpp @@ -18,10 +18,8 @@ #include #include #include -#include +#include #include -#include -#include #include #include #include @@ -57,18 +55,25 @@ do_session( // These objects perform our I/O tcp::resolver resolver{ioc}; - websocket::stream> ws{ioc, ctx}; + websocket::stream>> ws(ioc, ctx); // Look up the domain name auto const results = resolver.async_resolve(host, port, yield[ec]); if(ec) 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 - 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) 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 ws.next_layer().async_handshake(ssl::stream_base::client, yield[ec]); if(ec) diff --git a/example/websocket/client/coro/websocket_client_coro.cpp b/example/websocket/client/coro/websocket_client_coro.cpp index c4c7497e..df0d526a 100644 --- a/example/websocket/client/coro/websocket_client_coro.cpp +++ b/example/websocket/client/coro/websocket_client_coro.cpp @@ -15,9 +15,7 @@ #include #include -#include #include -#include #include #include #include @@ -51,15 +49,19 @@ do_session( // These objects perform our I/O tcp::resolver resolver{ioc}; - websocket::stream ws{ioc}; + websocket::stream< + beast::tcp_stream> ws(ioc); // Look up the domain name auto const results = resolver.async_resolve(host, port, yield[ec]); if(ec) 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 - net::async_connect(ws.next_layer(), results.begin(), results.end(), yield[ec]); + beast::async_connect(ws.next_layer(), results, yield[ec]); if(ec) return fail(ec, "connect");