Closed bethebest0622 closed 8 months ago
具体的例子可以参考 https://github.com/alibaba/yalantinglibs
要 reduced 后的 example 的话,看看 @qicosmos 和 @poor-circle 有没有时间吧
@bethebest0622 用yalantinglibs的coro_http_client 可以比较容易的实现你的需求
#include "ylt/coro_http/coro_http_client.hpp"
#include "ylt/struct_json/json_reader.h"
//定义json对应的结构体
struct trading_decimal_t {
std::string_view name;
std::string_view min_amount;
std::string_view maker_fee_rate;
std::string_view taker_fee_rate;
std::string_view pricing_name;
int pricing_decimal;
std::string_view trading_name;
int trading_decimal;
};
REFLECTION(trading_decimal_t, name, min_amount, maker_fee_rate, taker_fee_rate, pricing_name, pricing_decimal, trading_name, trading_decimal);
struct market_result{
int code;
trading_decimal_t data;
std::string_view message;
};
REFLECTION(market_result, code, data, message);
// http请求
async_simple::coro::Lazy<void> test_async_ssl_client(
coro_http::coro_http_client &client) {
client.init_ssl("api.coinex.com");
std::string uri = "https://api.coinex.com/v1/market/detail?market=BTCUSDT";
auto data = co_await client.async_get(uri);
if(data.status==200){
market_result result;
struct_json::from_json(result, data.resp_body); // 解析json
}
}
int main() {
coro_http::coro_http_client client{};
async_simple::coro::syncAwait(test_async_ssl_client(client));
}
@bethebest0622 如果不是ssl的,则无需调用init ssl相关的代码。 test_async_ssl_client() 请求http 服务 整个过程是异步的。
@qicosmos 谢谢回复,您提供的例子,init_ssl显示找不到函数定义 去掉init_ssl的话,返回404
是不是我需要安装指定版本的库
@qicosmos 谢谢回复,您提供的例子,init_ssl显示找不到函数定义 去掉init_ssl的话,返回404
是不是我需要安装指定版本的库
你没安装openssl吧,换个http的网站看看
@qicosmos 谢谢回复,您提供的例子,init_ssl显示找不到函数定义 去掉init_ssl的话,返回404
是不是我需要安装指定版本的库
还有记得#define YLT_ENABLE_SSL
@poor-circle
这个放在哪个文件呢 放在cinatra 自动下载的时候好像会被覆盖
#define YLT_ENABLE_SSL
#include "ylt/coro_http/coro_http_client.hpp"
#include "ylt/struct_json/json_reader.h"
//定义json对应的结构体
struct trading_decimal_t {
std::string_view name;
std::string_view min_amount;
std::string_view maker_fee_rate;
std::string_view taker_fee_rate;
std::string_view pricing_name;
int pricing_decimal;
std::string_view trading_name;
int trading_decimal;
};
REFLECTION(trading_decimal_t, name, min_amount, maker_fee_rate, taker_fee_rate, pricing_name, pricing_decimal, trading_name, trading_decimal);
struct market_result{
int code;
trading_decimal_t data;
std::string_view message;
};
REFLECTION(market_result, code, data, message);
// http请求
async_simple::coro::Lazy<void> test_async_ssl_client(
coro_http::coro_http_client &client) {
client.init_ssl("api.coinex.com");
std::string uri = "https://api.coinex.com/v1/market/detail?market=BTCUSDT";
auto data = co_await client.async_get(uri);
if(data.status==200){
market_result result;
struct_json::from_json(result, data.resp_body); // 解析json
}
}
int main() {
coro_http::coro_http_client client{};
async_simple::coro::syncAwait(test_async_ssl_client(client));
}
@poor-circle
感谢您,不过可否看下下面这个例子,我的意图是 主线程连续调用http get请求,希望前面的get网络延迟的时候,不要阻塞后面的get,并可以正确处理所有返回的json,希望能比阻塞调用获得更快的响应
#define YLT_ENABLE_SSL
#include "curl_util/curl_util.h"
#include "util/time_util.h"
#include "ylt/coro_http/coro_http_client.hpp"
#include "ylt/struct_json/json_reader.h"
//定义json对应的结构体
struct trading_decimal_t {
std::string_view name;
std::string_view min_amount;
std::string_view maker_fee_rate;
std::string_view taker_fee_rate;
std::string_view pricing_name;
int pricing_decimal;
std::string_view trading_name;
int trading_decimal;
};
REFLECTION(trading_decimal_t, name, min_amount, maker_fee_rate, taker_fee_rate,
pricing_name, pricing_decimal, trading_name, trading_decimal);
struct market_result {
int code;
trading_decimal_t data;
std::string_view message;
};
REFLECTION(market_result, code, data, message);
// http请求
async_simple::coro::Lazy<void>
test_async_ssl_client(coro_http::coro_http_client &client, int i) {
util::time_util::Timer t; t.StartTimer();
std::string uri = "https://api.coinex.com/v1/market/detail?market=BTCUSDT";
auto data = co_await client.async_get(uri);
if (data.status == 200) {
market_result result;
struct_json::from_json(result, data.resp_body); // 解析json
std::cout << result.data.maker_fee_rate << std::endl;
t.EndTimer(std::to_string(i) + " asd");
} else {
std::cout << "failed " << data.resp_body << " " << data.net_err << " "
<< " " << data.status << std::endl;
}
}
int main() {
coro_http::coro_http_client client{};
auto a = client.init_ssl("api.coinex.com");
std::cout << a << std::endl;
for (size_t i = 0; i < 10; ++i)
const auto & asd =test_async_ssl_client(client, i);
// async_simple::coro::syncAwait(test_async_ssl_client(client, i));
}
@poor-circle
感谢您,不过可否看下下面这个例子,我的意图是 主线程连续调用http get请求,希望前面的get网络延迟的时候,不要阻塞后面的get,并可以正确处理所有返回的json,希望能比阻塞调用获得更快的响应
#define YLT_ENABLE_SSL #include "curl_util/curl_util.h" #include "util/time_util.h" #include "ylt/coro_http/coro_http_client.hpp" #include "ylt/struct_json/json_reader.h" //定义json对应的结构体 struct trading_decimal_t { std::string_view name; std::string_view min_amount; std::string_view maker_fee_rate; std::string_view taker_fee_rate; std::string_view pricing_name; int pricing_decimal; std::string_view trading_name; int trading_decimal; }; REFLECTION(trading_decimal_t, name, min_amount, maker_fee_rate, taker_fee_rate, pricing_name, pricing_decimal, trading_name, trading_decimal); struct market_result { int code; trading_decimal_t data; std::string_view message; }; REFLECTION(market_result, code, data, message); // http请求 async_simple::coro::Lazy<void> test_async_ssl_client(coro_http::coro_http_client &client, int i) { util::time_util::Timer t; t.StartTimer(); std::string uri = "https://api.coinex.com/v1/market/detail?market=BTCUSDT"; auto data = co_await client.async_get(uri); if (data.status == 200) { market_result result; struct_json::from_json(result, data.resp_body); // 解析json std::cout << result.data.maker_fee_rate << std::endl; t.EndTimer(std::to_string(i) + " asd"); } else { std::cout << "failed " << data.resp_body << " " << data.net_err << " " << " " << data.status << std::endl; } } int main() { coro_http::coro_http_client client{}; auto a = client.init_ssl("api.coinex.com"); std::cout << a << std::endl; for (size_t i = 0; i < 10; ++i) const auto & asd =test_async_ssl_client(client, i); // async_simple::coro::syncAwait(test_async_ssl_client(client, i)); }
你把这个issue移到yalantinglibs重新开一个吧
好的
比如
curl -X GET https://api.coinex.com/v1/market/detail\?market\=BTCUSDT
可以得到远程服务器的一个数据返回,但是curl是同步操作会阻塞一些时间(网络延时)有办法异步处理多个get请求,并且在回调函数中处理返回(json数据)吗?
如果方便,可否在demo中添加这个例子,十分感谢