我正在尝试启动并运行 Rust WebSocket 服务器。我从 example code for an async websocker server 开始.
每次我得到一个Io
错误,比如连接中断,整个程序结束,没有任何错误。我将示例第 26 行的代码修改为:
.map_err(|InvalidConnection {error, ..}| {
println!("Error:{:?}",error);
return error;
})
这会打印错误,但不会阻止程序仅停止单个连接而不会自行崩溃。
我遇到的最常见错误是:
Io(Error { repr: Custom(Custom { kind: Other, error: Stream(Error { repr: Custom(Custom { kind: ConnectionAborted, error: StringError("unexpected EOF observed") }) }) }) })
最佳答案
好的,在 rust IRC channel 的帮助下,我找到了解决方案。我刚刚将错误替换为 None 并将其过滤掉。
.map(Some).or_else(|_| -> Result<_, ()> { Ok(None) }).filter_map(|x| x)
也许此信息可以帮助遇到类似问题的人。
关于asynchronous - 使用 rust-websocket 时如何处理错误,以便只有该连接失败而不是整个程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44974034/