Comments (11)
具体的例子可以参考 https://github.com/alibaba/yalantinglibs
要 reduced 后的 example 的话,看看 @qicosmos 和 @poor-circle 有没有时间吧
from async_simple.
@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));
}
from async_simple.
@bethebest0622 如果不是ssl的,则无需调用init ssl相关的代码。
test_async_ssl_client() 请求http 服务 整个过程是异步的。
from async_simple.
@qicosmos
谢谢回复,您提供的例子,init_ssl显示找不到函数定义
去掉init_ssl的话,返回404
是不是我需要安装指定版本的库
from async_simple.
@qicosmos 谢谢回复,您提供的例子,init_ssl显示找不到函数定义 去掉init_ssl的话,返回404
是不是我需要安装指定版本的库
你没安装openssl吧,换个http的网站看看
from async_simple.
@qicosmos 谢谢回复,您提供的例子,init_ssl显示找不到函数定义 去掉init_ssl的话,返回404
是不是我需要安装指定版本的库
还有记得#define YLT_ENABLE_SSL
from async_simple.
@poor-circle
#define #define YLT_ENABLE_SSL
这个放在哪个文件呢 放在cinatra 自动下载的时候好像会被覆盖
from async_simple.
#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));
}
from async_simple.
感谢您,不过可否看下下面这个例子,我的意图是 主线程连续调用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));
}
from async_simple.
感谢您,不过可否看下下面这个例子,我的意图是 主线程连续调用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重新开一个吧
from async_simple.
好的
from async_simple.
Related Issues (20)
- We need functionality similar to asio::dispatch HOT 1
- xmake build failed on windows HOT 4
- Deploy static content to Pages task is getting failure HOT 3
- a bug in specifying executors for Future HOT 11
- What about add a new feature lazy_local HOT 6
- Perhaps more information can be exposed to the Executor to guide scheduling HOT 13
- Try.h报错信息拼写错误
- co_await async_simple::coro::sleep() question HOT 4
- how to resume a Yield coro HOT 2
- ConditionVariable::notifyOne 可能miss唤醒,
- 請問如何實現類似epoll的模型? HOT 4
- There might be better to use like `asyncio.gather` in example of `StacklessCoroutineAndFuture.md` HOT 1
- coro::ConditionVariable is not aware of Executor HOT 3
- A question about Mutex implementation HOT 3
- GTEST_INCLUDE_DIR-NOTFOUND HOT 2
- how to adapt to callback style API HOT 3
- Bug Report: Extra Increment on Generator::iterator After Completion Causes Crash Using Clang15 HOT 6
- What are the best practices for cancellation? HOT 1
- 相比于老式的异步编程, switch延时如何? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from async_simple.