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