Update example to not broadcast close messages.

In the event that the client sends a close message,
it is broadcast to other clients closing them as well.
pull/26/head
Mark Lodato 5 years ago committed by Sebastian Dröge
parent 6bffd862b6
commit 70c77160a9
  1. 46
      examples/server.rs

@ -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);

Loading…
Cancel
Save