我正在尝试构建一个处理异步 Websocket 通信的结构。我正在努力设置连接,作为其中的一部分,我正在使用 tokio_tungstenite::connect::connect_async()。我想将其返回值存储为我正在处理的结构中的字段。
我尝试过以下方法:
use tokio_tungstenite::tungstenite::stream::MaybeTlsStream;
use tokio_tungstenite::WebSocketStream;
use tokio_tungstenite::connect_async;
use crate::WrAuth;
use crate::net::wr_ip_addr::WrIPAddress;
pub struct WrAsyncSocket {
m_auth: WrAuth,
m_ip_address: WrIPAddress,
m_socket_stream: Option<WebSocketStream<MaybeTlsStream<TcpStream>>>,
m_require_all: bool,
}
impl WrAsyncSocket {
pub fn new(x_auth: WrAuth, x_ip_address: WrIPAddress, x_socket_stream: Option<WebSocketStream<MaybeTlsStream<TcpStream>>>, x_require_all: bool) -> Self {
Self {
m_auth: x_auth,
m_ip_address: x_ip_address,
m_socket_stream: x_socket_stream,
m_require_all: x_require_all,
}
}
pub async fn connect_async(&mut self) {
let (ws_stream, _) = connect_async(self.m_ip_address.clone().get_url()).await.expect("Failed to connect.");
self.m_socket_stream = Some(ws_stream);
}
}
有了这个,我尝试导入两者
tokio::net::tcp::stream::TcpStream
和
tokio::net::TcpStream
但是,两者都会在 TcpStream 的任何实例上导致以下错误:
the trait bound `tokio::net::TcpStream: std::io::Read` is not satisfied
the trait `std::io::Read` is not implemented for `tokio::net::TcpStream`rustcClick for full compiler diagnostic
stream.rs(63, 28): required by a bound in `tokio_tungstenite::tungstenite::stream::MaybeTlsStream`
对于我应该在这里做什么有什么建议吗?附带说明:我是 Rust 和 tokio_tungstenite 的新手,因此非常感谢任何方面的帮助/提示。随心所欲地批评。