gpt4 book ai didi

rust - 如何访问Tokio-Tungstenite 0.10中的对等IP地址?

转载 作者:行者123 更新时间:2023-12-03 11:45:08 25 4
gpt4 key购买 nike

我将东京钨-钨从0.9升级到0.10,它不再具有peer_addr方法。我正在处理 WebSocketStream 。我已经尝试过了,但以前可以这样做:

use tokio_tungstenite::connect_async;

fn main() {
async fn test() {
println!("Hello, world!");
let url = "wss://echo.websocket.org".to_string();

let (ws_stream, ws_response) = connect_async(url).await.unwrap();
let addr = ws_stream.peer_addr();
println!("peer addr {:?}", addr);
}
}
peer_addr()不再存在,并且我看不到任何替代物或有关现在该怎么做的说明。我尝试搜索文档和GitHub。
Cargo.toml:
futures = { version = "0.3" }
tungstenite = {version="0.11.0", features=["tls"] }
tokio-tungstenite = { version="0.10.1", features = ["tls"] }
tokio = {version ="0.2.21", features = [ "full" ] }
url = "2.0"

最佳答案

documentation on docs.rsincorrect。如果您在本地构建文档,则会看到connect_async的正确签名:

pub async fn connect_async<R>(
request: R
) -> Result<(WebSocketStream<MaybeTlsStream<TcpStream>>, Response), Error>
where
R: IntoClientRequest + Unpin,
您需要访问 TcpStream才能访问对等地址:
use tokio_tungstenite::{connect_async, stream::Stream};

#[tokio::main]
async fn main() {
let (ws_stream, _) = connect_async("wss://echo.websocket.org").await.unwrap();

let tcp_stream = match ws_stream.get_ref() {
Stream::Plain(s) => s,
Stream::Tls(t) => t.get_ref(),
};

let addr = tcp_stream
.peer_addr()
.expect("connected streams should have a peer address");

println!("peer addr {:?}", addr);
}
Cargo.toml:
[dependencies]
tokio-tungstenite = { version = "0.10.1", features = ["tls"] }
tokio = { version = "0.2.21", features = [ "full" ] }
请注意,您原来的Cargo.toml包含 两种不同版本的钨矿,您确实不希望这样做。另请参见 Why is a trait not implemented for a type that clearly has it implemented?

关于rust - 如何访问Tokio-Tungstenite 0.10中的对等IP地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63024046/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com