-
I need send message only in websocket handler, and receive it in somewhere. So I use tokio watch(single-producer, multi-consumer.), But I can't pass the sender into the websocket handler function with data function as not impl clone trait. #[handler]
fn ws(
Path(name): Path<String>,
ws: WebSocket,
sender: Data<&tokio::sync::watch::Sender<String>>,
) -> impl IntoResponse {
ws.on_upgrade(move |socket| async move {
let (mut sink, mut stream) = socket.split();
tokio::spawn(async move {
while let Some(Ok(msg)) = stream.next().await {
if let Message::Text(text) = msg {
if sender.send(format!("{}: {}", name, text)).is_err() {
break;
}
}
}
});
})
} |
Beta Was this translation helpful? Give feedback.
Answered by
sunli829
Apr 23, 2022
Replies: 2 comments
-
You can wrap #[handler]
fn ws(
Path(name): Path<String>,
ws: WebSocket,
sender: Data<&Arc<tokio::sync::watch::Sender<String>>>,
) -> impl IntoResponse {
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
chanble
-
Would be very handy to include this somewhere in the documentation |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can wrap
tokio::sync::watch::Sender
withArc
so that it can be cloned.