从流中将数据转发到Tokio mpsc通道

时间:2017-08-09 15:28:31

标签: asynchronous rust rust-tokio

我正在尝试创建一个异步Rust客户端,它连接到一端的WebSocket服务器和另一端的蓝牙适配器。在两者之间会有一些过滤消息的逻辑。

我正在使用rust-websocket的WebSocket端异步模块。

我想隔离三个组件(websocket发送/接收,蓝牙发送/接收,消息处理)。为了做到这一点,我想使用<div class="w3-content w3-display-container"> <div id ="slider-wrapper"> <img id="img1" class="centerscreen mySlides slidepos fade" src="x.jpg" alt="picture1"> <img id="img2" class="centerscreen mySlides slidepos fade" src="xy.jpg" alt="picture2"> <img id="img3" class="centerscreen mySlides slidepos fade" src="xyz.jpg" alt="picture3"></div> <div class="w3-center w3-container w3-section w3-large w3-text-white centerscreen badgepos"> <span class="w3-badge demo w3-border w3-transparent w3-hover-white" onclick="currentDiv(1)"></span> <span class="w3-badge demo w3-border w3-transparent w3-hover-white" onclick="currentDiv(2)"></span> <span class="w3-badge demo w3-border w3-transparent w3-hover-white" onclick="currentDiv(3)"></span> </div> <container id="buttons" class="centerscreen"> <div id="navarrow-left" class="w3-left w3-hover-text-black btn" onclick="plusDivs(-1)">&#10094;</div> <div id="navarrow-right" class="w3-right w3-hover-text-black btn" onclick="plusDivs(1)">&#10095;</div> </container> </div> <div id="active"> Centerbutton </div>通道作为futures::sync::mpsc,以便将消息从WebSocket接收器传递到循环的消息处理部分。以下是我的代码的简化部分:

Sink

但是,当我运行此代码时,我收到几条错误消息,如下所示:

const CONNECTION: &'static str = "ws://127.0.0.1:4000/socket/websocket";

fn main() {
    let mut core = Core::new().unwrap();

    let (ws_send, ws_recv) = mpsc::channel(100);
    let (ws_to_main, main_from_ws) = mpsc::channel(100);

    let ws_future = ClientBuilder::new(CONNECTION)
        .unwrap()
        .add_protocol("rust-websocket")
        .async_connect_insecure(&core.handle())
        .and_then(|(duplex, _)| {
            let (mut sink, stream) = duplex.split();

            stream
                .filter_map(|message| {
                    println!("Received Message: {:?}", message);
                    match message {
                        OwnedMessage::Close(e) => Some(OwnedMessage::Close(e)),
                        OwnedMessage::Ping(d) => Some(OwnedMessage::Pong(d)),
                        OwnedMessage::Text(msg) => Some(OwnedMessage::Text(msg)),
                        _ => None,
                    }
                })
                .forward(ws_to_main)
        });
    let result = core.run(ws_future).unwrap();
}

我尝试使用error[E0277]: the trait bound `websocket::WebSocketError: std::convert::From<futures::sync::mpsc::SendError<websocket::OwnedMessage>>` is not satisfied --> src/main.rs:103:22 | 103 | .forward(ws_to_main) | ^^^^^^^ the trait `std::convert::From<futures::sync::mpsc::SendError<websocket::OwnedMessage>>` is not implemented for `websocket::WebSocketError` | = help: the following implementations were found: <websocket::WebSocketError as std::convert::From<std::io::Error>> <websocket::WebSocketError as std::convert::From<hyper::error::Error>> <websocket::WebSocketError as std::convert::From<websocket::client::ParseError>> <websocket::WebSocketError as std::convert::From<native_tls::Error>> and 5 others 将其错误地转换为.map_err,但SendError结构是私有的。

1 个答案:

答案 0 :(得分:0)

一种快速的解决方案是将蒸汽和水槽的错误类型转换为相同的类型:

stream
  .map_err(|err| eprintln!("stream error: {}", err))
  .forward(sink.sink_map_err(|err| eprintln!("sink error: {}", err)))