move http_client to a ws sub-command
This commit is contained in:
parent
846f0c680a
commit
c04bc3cdfc
@ -1,50 +0,0 @@
|
||||
/*
|
||||
* http_client.cpp
|
||||
* Author: Benjamin Sergeant
|
||||
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <ixwebsocket/IXHttpClient.h>
|
||||
|
||||
using namespace ix;
|
||||
|
||||
void run(const std::string& url)
|
||||
{
|
||||
HttpClient httpClient;
|
||||
bool verbose = true;
|
||||
auto out = httpClient.get(url, verbose);
|
||||
auto errorCode = std::get<0>(out);
|
||||
auto headers = std::get<1>(out);
|
||||
auto payload = std::get<2>(out);
|
||||
auto errorMsg = std::get<3>(out);
|
||||
|
||||
for (auto it : headers)
|
||||
{
|
||||
std::cout << it.first << ": " << it.second << std::endl;
|
||||
}
|
||||
|
||||
std::cout << "error code: " << errorCode << std::endl;
|
||||
if (!errorMsg.empty())
|
||||
{
|
||||
std::cout << "error message: " << errorMsg << std::endl;
|
||||
}
|
||||
|
||||
std::cout << "payload: " << payload << std::endl;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc != 2)
|
||||
{
|
||||
std::cerr << "Usage: ws_connect <url>" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
std::string url = argv[1];
|
||||
|
||||
Socket::init();
|
||||
run(url);
|
||||
return 0;
|
||||
}
|
@ -23,6 +23,7 @@ add_executable(ws
|
||||
ixcrypto/IXHash.cpp
|
||||
ixcrypto/IXUuid.cpp
|
||||
|
||||
ws_http_client.cpp
|
||||
ws_ping_pong.cpp
|
||||
ws_broadcast_server.cpp
|
||||
ws_echo_server.cpp
|
||||
|
@ -17,6 +17,8 @@
|
||||
|
||||
namespace ix
|
||||
{
|
||||
int ws_http_client_main(const std::string& url);
|
||||
|
||||
int ws_ping_pong_main(const std::string& url);
|
||||
|
||||
int ws_echo_server_main(int port);
|
||||
@ -73,6 +75,9 @@ int main(int argc, char** argv)
|
||||
CLI::App* pingPongApp = app.add_subcommand("ping", "Ping pong");
|
||||
pingPongApp->add_option("url", url, "Connection url")->required();
|
||||
|
||||
CLI::App* httpClientApp = app.add_subcommand("http_client", "HTTP Client");
|
||||
httpClientApp->add_option("url", url, "Connection url")->required();
|
||||
|
||||
CLI11_PARSE(app, argc, argv);
|
||||
|
||||
ix::Socket::init();
|
||||
@ -110,6 +115,10 @@ int main(int argc, char** argv)
|
||||
{
|
||||
return ix::ws_ping_pong_main(url);
|
||||
}
|
||||
else if (app.got_subcommand("http_client"))
|
||||
{
|
||||
return ix::ws_http_client_main(url);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
36
ws/ws_http_client.cpp
Normal file
36
ws/ws_http_client.cpp
Normal file
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* http_client.cpp
|
||||
* Author: Benjamin Sergeant
|
||||
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <ixwebsocket/IXHttpClient.h>
|
||||
|
||||
namespace ix
|
||||
{
|
||||
void ws_http_client_main(const std::string& url)
|
||||
{
|
||||
HttpClient httpClient;
|
||||
bool verbose = true;
|
||||
auto out = httpClient.get(url, verbose);
|
||||
auto errorCode = std::get<0>(out);
|
||||
auto headers = std::get<1>(out);
|
||||
auto payload = std::get<2>(out);
|
||||
auto errorMsg = std::get<3>(out);
|
||||
|
||||
for (auto it : headers)
|
||||
{
|
||||
std::cout << it.first << ": " << it.second << std::endl;
|
||||
}
|
||||
|
||||
std::cout << "error code: " << errorCode << std::endl;
|
||||
if (!errorMsg.empty())
|
||||
{
|
||||
std::cout << "error message: " << errorMsg << std::endl;
|
||||
}
|
||||
|
||||
std::cout << "payload: " << payload << std::endl;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user