WsClient.hpp
Go to the documentation of this file.
1 // @formatter:off
2 //
3 // Balau core C++ library
4 //
5 // Copyright (C) 2017 Bora Software (contact@borasoftware.com)
6 //
7 // Licensed under the Boost Software License - Version 1.0 - August 17th, 2003.
8 // See the LICENSE file for the full license text.
9 //
10 
16 
17 #ifndef COM_BORA_SOFTWARE__BALAU_NETWORK_HTTP_CLIENT__WS_CLIENT
18 #define COM_BORA_SOFTWARE__BALAU_NETWORK_HTTP_CLIENT__WS_CLIENT
19 
20 #include <Balau/Type/ToString.hpp>
21 
24 #include <Balau/Resource/Url.hpp>
25 #include <Balau/Type/StdTypes.hpp>
26 
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>
31 #include <cstdlib>
32 
33 namespace Balau::Network::Http {
34 
41 class WsClient {
48  public: explicit WsClient(std::string host_, unsigned short port_ = 80)
49  : host(std::move(host_))
50  , port(port_) {}
51 
55  public: WsClient(const WsClient &) = default;
56 
60  public: WsClient(WsClient &&) = default;
61 
65  public: WsClient & operator = (const WsClient &) = default;
66 
70  public: WsClient & operator = (WsClient &&) = default;
71 
72  public: template <typename ResponseBufferT, typename RequestBufferT>
73  ResponseBufferT send(const std::string_view & path, const RequestBufferT & requestData) {
75 
76  boost::asio::io_context ioc;
77  TCP::resolver resolver { ioc };
78  TCP::socket socket { ioc };
79 
80  auto resolverResults = resolver.resolve(host.c_str(), ::toString(port).c_str());
81  boost::asio::connect(socket, resolverResults.begin(), resolverResults.end());
82 
83  WS::stream<TCP::socket> ws { ioc };
84 
85  ws.handshake(host, path.empty() ? "/" : std::string(path));
86  ws.write(requestData);
87 
88  ResponseBufferT buffer {};
89 
90  ws.read(buffer);
91  ws.close(WS::close_code::normal);
92 
94 
95  boost::system::error_code errorCode {};
96  socket.shutdown(TCP::socket::shutdown_both, errorCode);
97 
98  // TODO clarify the following.
99  if (errorCode && errorCode != boost::system::errc::not_connected) {
100  throw boost::system::system_error { errorCode }; // TODO
101  }
102 
104 
105  return buffer;
106  }
107 
109 
110  private: std::string host;
111  private: unsigned short port;
112 };
113 
114 } // namespace Balau::Network::Http
115 
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.
STL namespace.
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.