17 #ifndef COM_BORA_SOFTWARE__BALAU_NETWORK_HTTP_CLIENT__WS_CLIENT 18 #define COM_BORA_SOFTWARE__BALAU_NETWORK_HTTP_CLIENT__WS_CLIENT 27 #include <boost/beast/core.hpp> 28 #include <boost/beast/websocket.hpp> 29 #include <boost/asio/connect.hpp> 30 #include <boost/asio/ip/tcp.hpp> 48 public:
explicit WsClient(std::string host_,
unsigned short port_ = 80)
49 : host(
std::move(host_))
72 public:
template <
typename ResponseBufferT,
typename RequestBufferT>
73 ResponseBufferT send(
const std::string_view & path,
const RequestBufferT & requestData) {
76 boost::asio::io_context ioc;
77 TCP::resolver resolver { ioc };
78 TCP::socket socket { ioc };
80 auto resolverResults = resolver.resolve(host.c_str(),
::toString(port).c_str());
81 boost::asio::connect(socket, resolverResults.begin(), resolverResults.end());
83 WS::stream<TCP::socket> ws { ioc };
85 ws.handshake(host, path.empty() ?
"/" : std::string(path));
86 ws.write(requestData);
88 ResponseBufferT buffer {};
91 ws.close(WS::close_code::normal);
95 boost::system::error_code errorCode {};
96 socket.shutdown(TCP::socket::shutdown_both, errorCode);
99 if (errorCode && errorCode != boost::system::errc::not_connected) {
100 throw boost::system::system_error { errorCode };
110 private: std::string host;
111 private:
unsigned short port;
116 #endif // COM_BORA_SOFTWARE__BALAU_NETWORK_HTTP_CLIENT__HTTPS_CLIENT Components and utilities working on HTTP data transmission.
Definition: HttpClient.hpp:26
Pre-defined universal to-string functions.
Low level types used in networking code.
WsClient(std::string host_, unsigned short port_=80)
Create a WebSocket client instance.
Definition: WsClient.hpp:48
Core includes, typedefs and functions.
An abstract Url (either http or https).
A simple WebSocket client.
Definition: WsClient.hpp:41
WsClient & operator=(const WsClient &)=default
Assign an HTTPS client by copying the supplied instance.
Balau::U8String< AllocatorT > toString(LoggingLevel level)
Print the logging level as a UTF-8 string.
Definition: LoggingLevel.hpp:73
Balau exceptions for network operations.