in chat example, how to reuse sender
as it is moved
#610
-
Hi there, in line 94 axum/examples/chat/src/main.rs Line 94 in b1623ce sender is moved:
// This task will receive broadcast messages and send text message to our client.
let mut send_task = tokio::spawn(async move {
while let Ok(msg) = rx.recv().await {
// In any websocket error, break loop.
if sender.send(Message::Text(msg)).await.is_err() {
break;
}
}
}); Because the sender can't be cloned, is it more idiomatic:
I realise this is more a general design question - I'm just interested in the idioms here :-) |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
Putting the Is your use case that you need to send things on the socket from multiple places in your code concurrently? |
Beta Was this translation helpful? Give feedback.
-
Exactly that.
… On 11 Dec 2021, at 04:47, David Pedersen ***@***.***> wrote:
Putting the WebSocket in an arc wouldn't help because sending requires &mut self which you cannot get through an arc.
Is your use case that you need to send things on the socket from multiple places in your code concurrently?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications on the go with GitHub Mobile for iOS or Android.
|
Beta Was this translation helpful? Give feedback.
-
I think I would make an mpsc channel and spawn a task to forward messages from the mpsc receiver to the socket sender. The mpsc sender can be cloned. |
Beta Was this translation helpful? Give feedback.
I think I would make an mpsc channel and spawn a task to forward messages from the mpsc receiver to the socket sender. The mpsc sender can be cloned.