如何从actix响应中逐步读取流?

时间:2019-07-15 14:35:46

标签: rust rust-actix

以下代码基于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);
    };
}

responseactix_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的类型。有人可以帮忙吗?

0 个答案:

没有答案