54 {
56
57 {
59 using Request = LegacyClient::Request;
60 using Response = LegacyClient::Response;
61 using LegacySocketAddress = LegacyClient::SocketAddress;
62
63 const LegacyClient legacyClient(
64 "legacy",
65 [](const std::shared_ptr<Request>& req) {
66 const std::string connectionName = req->getSocketContext()->getSocketConnection()->getConnectionName();
67
68 VLOG(1) << connectionName << ": OnRequestBegin";
69
70 req->set("Sec-WebSocket-Protocol", "subprotocol, echo");
71
72 req->upgrade(
73 "/ws",
74 "websocket",
75 [connectionName](const std::shared_ptr<Request>& req, bool success) {
76 VLOG(1) << connectionName << ": HTTP Upgrade (http -> " << req->header("upgrade") << "|"
77 << req->header("Sec-WebSocket-Protocol") << ") start " << (success ? "success" : "failed");
78 },
79 [connectionName]([[maybe_unused]] const std::shared_ptr<Request>& req,
80 [[maybe_unused]] const std::shared_ptr<Response>& res,
81 [[maybe_unused]] bool success) {
82 VLOG(1) << connectionName << ": Upgrade success:";
83
84 VLOG(1) << connectionName << ": Requested: " << req->header("upgrade");
85 VLOG(1) << connectionName << ": Selected: " << res->get("upgrade");
86 });
87 },
88 []([[maybe_unused]] const std::shared_ptr<Request>& req) {
89 const std::string connectionName = req->getSocketContext()->getSocketConnection()->getConnectionName();
90
91 VLOG(1) << connectionName << ": OnRequestEnd";
92 });
93
94 legacyClient.connect([instanceName = legacyClient.getConfig().getInstanceName()](const LegacySocketAddress& socketAddress,
96 switch (state) {
98 VLOG(1) << instanceName << " connected to '" << socketAddress.toString() << "'";
99 break;
101 VLOG(1) << instanceName << " disabled";
102 break;
104 VLOG(1) << instanceName << " " << socketAddress.toString() << ": " << state.what();
105 break;
107 VLOG(1) << instanceName << " " << socketAddress.toString() << ": " << state.what();
108 break;
109 }
110 });
111
113 using Request = TlsClient::Request;
114 using Response = TlsClient::Response;
115 using TLSSocketAddress = TlsClient::SocketAddress;
116
117 const TlsClient tlsClient(
118 "tls",
119 [](const std::shared_ptr<Request>& req) {
120 const std::string connectionName = req->getSocketContext()->getSocketConnection()->getConnectionName();
121
122 VLOG(1) << connectionName << ": OnRequestBegin";
123
124 req->set("Sec-WebSocket-Protocol", "subprotocol, echo");
125
126 req->upgrade(
127 "/ws",
128 "websocket",
129 [connectionName](const std::shared_ptr<Request>& req, bool success) {
130 VLOG(1) << connectionName << ": HTTP Upgrade (http -> " << req->header("upgrade") << "|"
131 << req->header("Sec-WebSocket-Protocol") << ") start " << (success ? "success" : "failed");
132 },
133 [connectionName]([[maybe_unused]] const std::shared_ptr<Request>& req,
134 [[maybe_unused]] const std::shared_ptr<Response>& res,
135 [[maybe_unused]] bool success) {
136 });
137 },
138 []([[maybe_unused]] const std::shared_ptr<Request>& req) {
139 const std::string connectionName = req->getSocketContext()->getSocketConnection()->getConnectionName();
140
141 VLOG(1) << connectionName << ": OnRequestEnd";
142 });
143
144 tlsClient.connect([instanceName = tlsClient.getConfig().getInstanceName()](const TLSSocketAddress& socketAddress,
146 switch (state) {
148 VLOG(1) << instanceName << " connected to '" << socketAddress.toString() << "'";
149 break;
151 VLOG(1) << instanceName << " disabled";
152 break;
154 VLOG(1) << instanceName << " " << socketAddress.toString() << ": " << state.what();
155 break;
157 VLOG(1) << instanceName << " " << socketAddress.toString() << ": " << state.what();
158 break;
159 }
160 });
161 }
162
164}
static void init(int argc, char *argv[])
static int start(const utils::Timeval &timeOut={LONG_MAX, 0})
static constexpr int DISABLED
static constexpr int ERROR
static constexpr int FATAL
Client::Response Response
web::http::client::Client< net::in::stream::legacy::SocketClient > Client
web::http::client::Client< net::in::stream::tls::SocketClient > Client