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

Loading…
Cancel
Save