以下代码基于actix-web v1.0.3:
use actix;
use actix_web::client::Client;
use actix_web::client::ClientResponse;
use futures::future::Future;
use futures::future::IntoFuture;
use futures::stream::Stream;
use std::result::Result;
pub fn connect() {
let client = Client::default();
if let Err(e) = actix::run(move || {
client
.get("http://127.0.0.1:8088/streaming")
.send()
.map_err(|_| ())
.then(|response| {
response.into_future().then(|x| {
println!("{:?}", x);
x.into_future().and_then(|x| {
println!("{:?}", x);
Ok(())
})
})
})
}) {
println!("Error : {:?}", e);
};
}
response
是actix_web::client::ClientResponse<impl futures::stream::Stream>
的类型,它实现了Stream
特征(doc)。
问题:如何在收到整个响应之前逐步读取流?
我想声明要重复触发的回调函数,但无法编译。
pub fn connect() {
let client = Client::default();
if let Err(e) = actix::run(move || {
client
.get("http://127.0.0.1:8088/streaming")
.send()
.map_err(|_| ())
.then(|response| {
response.into_future().then(onResponse)
})
}) {
println!("Error : {:?}", e);
};
}
fn onResponse(result: Result<ClientResponse<impl Stream>, ()>) -> Future<Item = ClientResponse<impl Stream>, Error = ()> {
result.into_future().then(onResponse)
}
确实不知道Item
的{{1}}和Error
的类型。有人可以帮忙吗?