44#include < boost/asio/connect.hpp>
55#include < boost/asio/detached.hpp>
66#include < boost/asio/io_context.hpp>
7+ #include < boost/asio/ip/tcp.hpp>
78#include < boost/asio/spawn.hpp>
89#include < boost/asio/ssl/context.hpp>
910#include < boost/asio/ssl/stream.hpp>
1011#include < boost/asio/strand.hpp>
11- #include < boost/asio/ip/tcp.hpp>
1212#include < boost/asio/version.hpp>
1313#include < http_async.h>
1414#include " extra/CLI11.hpp"
@@ -34,22 +34,29 @@ using namespace std::chrono_literals;
3434// ----------------------------------------------------------------------------------------------------------------
3535// ----------------------------------------------------------------------------------------------------------------
3636
37- void ws_session (std::string host, uint16_t port, std::string msg, yield_context_strand yield)
37+ template <class Sock >
38+ void read_write_one (Sock& sock, std::string_view host, std::vector<char >& buf, yield_context_strand yield)
39+ {
40+ constexpr bool is_server{false };
41+ constexpr bool is_text{false };
42+ http::async_ws_handshake (sock, host, " /ws" , yield);
43+ http::async_ws_write (sock, buf, is_text, is_server, yield);
44+ http::async_ws_read (sock, buf, is_server, yield);
45+ http::async_ws_close (sock, http::ws_going_away, is_server, yield);
46+ }
47+
48+ void ws_session (std::string_view host, uint16_t port, std::string_view msg, yield_context_strand yield)
3849{
3950 try
4051 {
41- // Connect
42- tcp_socket sock (yield.get_executor ());
43- tcp::resolver resolver (sock.get_executor ());
44- std::vector<char > buf (begin (msg), end (msg));
45- size_t ret{};
52+ // Objects
53+ tcp_socket sock (yield.get_executor ());
54+ tcp::resolver resolver (sock.get_executor ());
55+ std::vector<char > buf (begin (msg), end (msg));
4656
4757 // Async IO
4858 boost::asio::async_connect (sock, resolver.async_resolve (host, std::to_string (port), yield), boost::asio::cancel_after (5s, yield));
49- http::async_ws_handshake (sock, host, " /ws" , yield);
50- ret = http::async_ws_write (sock, buf, true , false , yield);
51- ret = http::async_ws_read (sock, buf, false , yield);
52- http::async_ws_close (sock, http::ws_going_away, false , yield);
59+ read_write_one (sock, host, buf, yield);
5360
5461 // Print echo
5562 printf (" Server echoed back\n\" %.*s\"\n " , (int )buf.size (), buf.data ());
@@ -61,32 +68,27 @@ void ws_session(std::string host, uint16_t port, std::string msg, yield_context_
6168 }
6269}
6370
64- void ws_ssl_session (std::string host, uint16_t port, std::string msg, yield_context_strand yield)
71+ void ws_ssl_session (std::string_view host, uint16_t port, std::string_view msg, yield_context_strand yield)
6572{
6673 try
6774 {
68- // SSL
75+ // Objects
6976 boost::asio::ssl::context ssl (boost::asio::ssl::context::tlsv12_client);
7077 ssl.set_verify_callback ([](bool preverified, boost::asio::ssl::verify_context& ctx) {return true ;});
7178 ssl.set_verify_mode (boost::asio::ssl::verify_peer);
7279
73- // Connect
74- tls_socket sock (tcp_socket (yield.get_executor ()), ssl);
75- tcp::resolver resolver (sock.get_executor ());
76- std::vector<char > buf (begin (msg), end (msg));
77- size_t ret{};
80+ tls_socket sock (tcp_socket (yield.get_executor ()), ssl);
81+ tcp::resolver resolver (sock.get_executor ());
82+ std::vector<char > buf (begin (msg), end (msg));
7883
7984 // Async IO
8085 boost::asio::async_connect (sock.next_layer (), resolver.async_resolve (host, std::to_string (port), yield), boost::asio::cancel_after (5s, yield));
8186 sock.async_handshake (boost::asio::ssl::stream_base::client, yield);
82- http::async_ws_handshake (sock, host, " /ws" , yield);
83- ret = http::async_ws_write (sock, buf, true , false , yield);
84- ret = http::async_ws_read (sock, buf, false , yield);
85- http::async_ws_close (sock, http::ws_going_away, false , yield);
87+ read_write_one (sock, host, buf, yield);
8688 sock.async_shutdown (yield);
8789
8890 // Print echo
89- printf (" Server echoed back\n\" %.*s\"\n " , (int )buf.size (), buf.data ());
91+ printf (" TLS server echoed back\n\" %.*s\"\n " , (int )buf.size (), buf.data ());
9092 }
9193 catch (const boost::system::system_error& e)
9294 {
@@ -100,7 +102,7 @@ int main(int argc, char* argv[])
100102 std::string host;
101103 uint16_t port;
102104 std::string msg;
103- bool use_tls;
105+ bool use_tls{} ;
104106 CLI::App app{" WebSocket echo client" };
105107 try {
106108 app.add_option (" --host" , host, " Host or IP address of WebSocket server" )->required ();
0 commit comments