3 #include <boost/beast/core.hpp>
4 #include <boost/beast/websocket.hpp>
5 #include <boost/beast/ssl.hpp>
6 #include <boost/asio/connect.hpp>
7 #include <boost/asio/ip/tcp.hpp>
8 #include <boost/asio/ssl/stream.hpp>
21 using fail_handler = std::function<void(
const boost::beast::error_code&)>;
37 void connect(
const std::string& uri);
38 void send(
const std::string& message);
45 void on_resolve(
const boost::beast::error_code& ec,
46 boost::asio::ip::tcp::resolver::results_type results);
47 void on_connect(
const boost::beast::error_code& ec);
50 void on_write(
const boost::beast::error_code& ec, std::size_t bytes_transferred);
51 void on_read(
const boost::beast::error_code& ec, std::size_t bytes_transferred);
52 void on_close(
const boost::beast::error_code& ec);
55 void fail(
const boost::beast::error_code& ec);
58 boost::asio::io_context
ioc_;
61 std::unique_ptr<boost::beast::websocket::stream<boost::beast::ssl_stream<boost::asio::ip::tcp::socket>>>
wss_stream_;
62 std::unique_ptr<boost::beast::websocket::stream<boost::asio::ip::tcp::socket>>
ws_stream_;
76 std::shared_ptr<boost::beast::flat_buffer>
buffer;
86 Uri(
const std::string& uri);
Definition: websocket_client.hh:84
std::string path
Definition: websocket_client.hh:93
std::string port
Definition: websocket_client.hh:92
std::string to_string() const
Definition: websocket_client.cc:284
std::string host
Definition: websocket_client.hh:91
std::string protocol
Definition: websocket_client.hh:90
Uri(const std::string &uri)
Definition: websocket_client.cc:270
Definition: websocket_client.hh:15
websocket_client(const websocket_client &)=delete
bool is_ssl_
Definition: websocket_client.hh:64
boost::beast::flat_buffer buffer_
Definition: websocket_client.hh:63
void set_message_handler(message_handler h)
Definition: websocket_client.cc:31
~websocket_client()
Definition: websocket_client.cc:13
void on_connect(const boost::beast::error_code &ec)
Definition: websocket_client.cc:94
void do_read()
Definition: websocket_client.cc:195
void on_ssl_handshake(const boost::beast::error_code &ec)
Definition: websocket_client.cc:113
void connect(const std::string &uri)
Definition: websocket_client.cc:36
boost::asio::io_context ioc_
Definition: websocket_client.hh:58
void fail(const boost::beast::error_code &ec)
Definition: websocket_client.cc:252
void on_read(const boost::beast::error_code &ec, std::size_t bytes_transferred)
Definition: websocket_client.cc:213
void on_handshake(const boost::beast::error_code &ec)
Definition: websocket_client.cc:123
void on_close(const boost::beast::error_code &ec)
Definition: websocket_client.cc:243
close_handler close_handler_
Definition: websocket_client.hh:70
std::string host_
Definition: websocket_client.hh:65
std::string port_
Definition: websocket_client.hh:65
void set_close_handler(close_handler h)
Definition: websocket_client.cc:33
std::queue< queued_message > message_queue_
Definition: websocket_client.hh:78
std::function< void()> close_handler
Definition: websocket_client.hh:20
void on_resolve(const boost::beast::error_code &ec, boost::asio::ip::tcp::resolver::results_type results)
Definition: websocket_client.cc:69
std::function< void()> connect_handler
Definition: websocket_client.hh:19
std::string path_
Definition: websocket_client.hh:65
std::unique_ptr< boost::beast::websocket::stream< boost::beast::ssl_stream< boost::asio::ip::tcp::socket > > > wss_stream_
Definition: websocket_client.hh:61
websocket_client & operator=(const websocket_client &)=delete
void run()
Definition: websocket_client.cc:259
websocket_client()
Definition: websocket_client.cc:4
void on_write(const boost::beast::error_code &ec, std::size_t bytes_transferred)
Definition: websocket_client.cc:153
bool writing_
Definition: websocket_client.hh:80
void close()
Definition: websocket_client.cc:225
boost::asio::ssl::context ssl_ctx_
Definition: websocket_client.hh:59
std::unique_ptr< boost::beast::websocket::stream< boost::asio::ip::tcp::socket > > ws_stream_
Definition: websocket_client.hh:62
message_handler message_handler_
Definition: websocket_client.hh:68
void set_fail_handler(fail_handler h)
Definition: websocket_client.cc:34
std::function< void(const std::string &)> message_handler
Definition: websocket_client.hh:18
fail_handler fail_handler_
Definition: websocket_client.hh:71
connect_handler connect_handler_
Definition: websocket_client.hh:69
void do_write()
Definition: websocket_client.cc:169
std::function< void(const boost::beast::error_code &)> fail_handler
Definition: websocket_client.hh:21
void set_connect_handler(connect_handler h)
Definition: websocket_client.cc:32
void send(const std::string &message)
Definition: websocket_client.cc:136
boost::asio::ip::tcp::resolver resolver_
Definition: websocket_client.hh:60
void stop()
Definition: websocket_client.cc:264
Definition: websocket_client.hh:74
std::string data
Definition: websocket_client.hh:75
std::shared_ptr< boost::beast::flat_buffer > buffer
Definition: websocket_client.hh:76