|
|
@ -52,26 +52,32 @@ async fn handle_connection(peer_map: PeerMap, raw_stream: TcpStream, addr: Socke |
|
|
|
|
|
|
|
|
|
|
|
let (outgoing, incoming) = ws_stream.split(); |
|
|
|
let (outgoing, incoming) = ws_stream.split(); |
|
|
|
|
|
|
|
|
|
|
|
let broadcast_incoming = incoming.try_for_each(|msg| { |
|
|
|
let broadcast_incoming = incoming |
|
|
|
println!( |
|
|
|
.try_filter(|msg| { |
|
|
|
"Received a message from {}: {}", |
|
|
|
// Broadcasting a Close message from one client
|
|
|
|
addr, |
|
|
|
// will close the other clients.
|
|
|
|
msg.to_text().unwrap() |
|
|
|
future::ready(!msg.is_close()) |
|
|
|
); |
|
|
|
}) |
|
|
|
let peers = peer_map.lock().unwrap(); |
|
|
|
.try_for_each(|msg| { |
|
|
|
|
|
|
|
println!( |
|
|
|
// We want to broadcast the message to everyone except ourselves.
|
|
|
|
"Received a message from {}: {}", |
|
|
|
let broadcast_recipients = peers |
|
|
|
addr, |
|
|
|
.iter() |
|
|
|
msg.to_text().unwrap() |
|
|
|
.filter(|(peer_addr, _)| peer_addr != &&addr) |
|
|
|
); |
|
|
|
.map(|(_, ws_sink)| ws_sink); |
|
|
|
let peers = peer_map.lock().unwrap(); |
|
|
|
|
|
|
|
|
|
|
|
for recp in broadcast_recipients { |
|
|
|
// We want to broadcast the message to everyone except ourselves.
|
|
|
|
recp.unbounded_send(msg.clone()).unwrap(); |
|
|
|
let broadcast_recipients = peers |
|
|
|
} |
|
|
|
.iter() |
|
|
|
|
|
|
|
.filter(|(peer_addr, _)| peer_addr != &&addr) |
|
|
|
future::ok(()) |
|
|
|
.map(|(_, ws_sink)| ws_sink); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
for recp in broadcast_recipients { |
|
|
|
|
|
|
|
recp.unbounded_send(msg.clone()).unwrap(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
future::ok(()) |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
let receive_from_others = rx.map(Ok).forward(outgoing); |
|
|
|
let receive_from_others = rx.map(Ok).forward(outgoing); |
|
|
|
|
|
|
|
|
|
|
|