async fn socket(mut ws: WebSocket, state: Users) {
tokio::spawn(async move {
while let Some(msg) = reader.recv().await{
println!("message for user: {:?}", msg);
ws.send(msg).await.unwrap();
};
});
while let Some(msg) = ws.recv().await{
// not matter
}
}
I need to use the ws value several times, once in a stream (tokio::spawn) and once in a loop, but the compiler writes that value is used after moved.
I moved the loop to the thread, but then the function does not receive the message (does not write a “message for user”):
tokio::spawn(async move {
while let Some(msg) = reader.recv().await{
println!("message for user: {:?}", msg);
ws.send(msg).await.unwrap();
};
while let Some(msg) = ws.recv().await{
//not matter
}
});
in the example from where I copied the code, ws.next() is used, but the compiler also throws an error on this: “next() -is not defined for WebSokcet”