不幸的是,显示回显服务器的示例对我没有帮助。如何发送和接收消息?
我已经阅读了文档,但它是无望的。文档中几乎所有的描述都需要先验知识。
tx.send(warp::ws::Message::text("test"))
这不起作用:
type mismatch resolving `<futures::sink::Send<'_, futures::stream::SplitSink<warp::ws::WebSocket, warp::ws::Message>, warp::ws::Message> as futures::Future>::Output == ()`
expected enum `std::result::Result`, found `()`
更新: 我可以找到一个解决方案(示例代码):
use futures::{SinkExt, StreamExt};
let ws_test = warp::path!("wbtest")
.and(warp::ws())
.map(|ws: warp::ws::Ws| {
ws.on_upgrade(|websocket| wb_handle(websocket))
});
async fn wb_handle(websocket: warp::ws::WebSocket) {
let (mut tx, mut rx) = websocket.split();
while let Some(result) = rx.next().await {
let msg = match result {
Ok(msg) => msg,
Err(e) => {
eprintln!("websocket error: {}", e);
break;
}
};
println!("msg: {:?}", msg);
let _ = tx.send(warp::ws::Message::text("hui!")).await;
}
}