我想将字符串发布到服务器并反序列化它并通过 rust lang 中的包装发回 JSON 对象。但每次我这样做时都会出现其他问题,我不知道如何解决它!它适用于字符串,但不会将 JSON 消息发送到 WebSocket 客户端
这是我的代码:
#[derive(Deserialize, Debug)]
pub struct Event {
topic: String,
user_id: Option<usize>,
message: String,
password: String,
}
pub async fn publish_handler(body: Event, clients: Clients) -> Result<impl Reply> {
if body.password != "password" {
Ok(StatusCode::from_u16(403).unwrap())
} else {
clients
.read()
.await
.iter()
.filter(|(_, client)| match body.user_id {
Some(v) => client.user_id == v,
None => true,
})
.filter(|(_, client)| client.topics.contains(&body.topic))
.for_each(|(_, client)| {
if let Some(sender) = &client.sender {
let json_response: Value = serde_json::from_str(&body.message.clone()).unwrap();
let _ = sender.send(Ok(Message::text(json_response)));
}
});
Ok(StatusCode::OK)
}
}
错误
我得到以下错误:
the trait bound `std::string::String: std::convert::From<serde_json::Value>` is not satisfied
依赖
[dependencies]
tokio = { version = "1.6.0", features = ["full"] }
tokio-stream = "0.1.6"
warp = "0.3.1"
serde = {version = "1.0", features = ["derive"] }
serde_json = "1.0"
futures = { version = "0.3", default-features = false }
uuid = { version = "0.8.2", features = ["serde", "v4"] }
reqwest = { version = "0.11", features = ["json"] }