10 #ifndef SUPPORTLIB_WEBSOCKETCLIENT_H
11 #define SUPPORTLIB_WEBSOCKETCLIENT_H
14 #include <boost/asio/ip/tcp.hpp>
15 #include <boost/beast/core.hpp>
16 #include <boost/beast/websocket.hpp>
17 #include <boost/beast/websocket/ssl.hpp>
18 #include <boost/asio/connect.hpp>
19 #include <boost/asio/ssl/stream.hpp>
26 using tcp = boost::asio::ip::tcp;
27 namespace ssl = boost::asio::ssl;
28 namespace websocket = boost::beast::websocket;
37 using SPtr = std::shared_ptr<WebSocketClientException>;
38 using UPtr = std::unique_ptr<WebSocketClientException>;
39 using WPtr = std::weak_ptr<WebSocketClientException>;
102 WebSocketClient(
const std::string& host,
const std::string& port,
bool ssl =
false,
const size_t numThreads = 1,
const std::string& resource =
"/") :
105 m_Resource(resource),
109 auto const results = m_Resolver.resolve(m_Host, m_Port);
112 boost::asio::connect(m_Wss.next_layer().next_layer(), results.begin(), results.end());
113 m_Wss.next_layer().handshake(ssl::stream_base::client);
114 m_Wss.handshake(host, m_Resource);
119 boost::asio::connect(m_Ws.next_layer(), results.begin(), results.end());
120 m_Ws.handshake(host, m_Resource);
123 m_Threads.reserve(numThreads);
124 for(
auto i = numThreads; i > 0; --i)
125 m_Threads.emplace_back(std::thread([
this]{ m_Ioc.run();}));
131 void send(
const std::string& msg){
134 {
if(m_Wss.is_open()){m_Wss.write(boost::asio::buffer(msg));}}
136 {
if(m_Ws.is_open()){m_Ws.write(boost::asio::buffer(msg));}}
174 {
if(m_Wss.is_open()){m_Wss.async_read(m_Buffer, std::bind(&WebSocketClient::on_read, this->shared_from_this(), std::placeholders::_1, std::placeholders::_2));}}
176 {
if(m_Ws.is_open()){m_Ws.async_read(m_Buffer, std::bind(&WebSocketClient::on_read, this->shared_from_this(), std::placeholders::_1, std::placeholders::_2));}}
185 {
if(m_Wss.is_open()){m_Wss.read(m_Buffer);}}
187 {
if(m_Ws.is_open()){m_Ws.read(m_Buffer);}}
188 m_Message = boost::beast::buffers_to_string(m_Buffer.data());
197 {
if(m_Wss.is_open()){m_Wss.close(websocket::close_code::normal);}}
199 {
if(m_Ws.is_open()){m_Ws.close(websocket::close_code::normal);}}
201 using SPtr = std::shared_ptr<WebSocketClient>;
202 using UPtr = std::unique_ptr<WebSocketClient>;
204 void on_read(boost::system::error_code ec, std::size_t bytes_transferred) {
208 m_Message = boost::beast::buffers_to_string(m_Buffer.data());
210 m_Buffer.consume(m_Buffer.size());
217 std::string m_Resource;
218 std::string m_Message;
220 boost::asio::io_context m_Ioc;
221 tcp::resolver m_Resolver{m_Ioc};
222 ssl::context m_Ctx{ssl::context::sslv23_client};
223 websocket::stream<tcp::socket> m_Ws{m_Ioc};
224 websocket::stream<ssl::stream<tcp::socket>> m_Wss{m_Ioc, m_Ctx};
225 boost::beast::multi_buffer m_Buffer;
226 boost::asio::executor_work_guard<boost::asio::io_context::executor_type> m_WG = boost::asio::make_work_guard(m_Ioc);
227 std::vector<std::thread> m_Threads;
228 boost::system::error_code m_Ec;
Base exception to inherit custom exceptions from.
Observer/Obersvable Pattern implementation.
Base exception to inherit custom exceptions from.
Definition: Exception.h:48
ExceptionBase(const std::string &msg="")
Definition: Exception.h:54
Observable class. Inherited classes can notify all classes which inherit from Observer.
Definition: Observer.h:99
void notify()
Definition: Observer.h:130
Exception to be thrown on websocket client errors.
Definition: WebSocketClient.h:34
Simple Websocket Client.
Definition: WebSocketClient.h:92
void close()
Definition: WebSocketClient.h:194
void run()
Definition: WebSocketClient.h:172
WebSocketClient(const std::string &host, const std::string &port, bool ssl=false, const size_t numThreads=1, const std::string &resource="/")
Definition: WebSocketClient.h:102
std::string getHost()
Definition: WebSocketClient.h:147
std::string getMessage()
Definition: WebSocketClient.h:141
bool getSSL()
Definition: WebSocketClient.h:159
std::string receive()
Definition: WebSocketClient.h:182
void send(const std::string &msg)
msg Message to send
Definition: WebSocketClient.h:131
boost::system::error_code getError()
Definition: WebSocketClient.h:165
std::string getPort()
Definition: WebSocketClient.h:153
Namespace for giri's C++ support library.
Definition: Base64.h:47