1
0
mirror of https://github.com/actix/examples synced 2024-11-23 22:41:07 +01:00

remove select macro from echo example

This commit is contained in:
Rob Ede 2022-07-11 20:19:20 +01:00
parent a4a060994d
commit fd17252725
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
9 changed files with 31 additions and 23 deletions

View File

@ -78,7 +78,7 @@ impl Actor for WsChatSession {
type Context = ws::WebsocketContext<Self>; type Context = ws::WebsocketContext<Self>;
fn started(&mut self, ctx: &mut Self::Context) { fn started(&mut self, ctx: &mut Self::Context) {
self.join_room("Main", ctx); self.join_room("main", ctx);
} }
fn stopped(&mut self, _ctx: &mut Self::Context) { fn stopped(&mut self, _ctx: &mut Self::Context) {

View File

@ -29,7 +29,7 @@ async fn chat_route(
WsChatSession { WsChatSession {
id: 0, id: 0,
hb: Instant::now(), hb: Instant::now(),
room: "Main".to_owned(), room: "main".to_owned(),
name: None, name: None,
addr: srv.get_ref().clone(), addr: srv.get_ref().clone(),
}, },

View File

@ -66,7 +66,7 @@ impl Default for ChatServer {
fn default() -> ChatServer { fn default() -> ChatServer {
// default room // default room
let mut rooms = HashMap::new(); let mut rooms = HashMap::new();
rooms.insert("Main".to_owned(), HashSet::new()); rooms.insert("main".to_owned(), HashSet::new());
ChatServer { ChatServer {
sessions: HashMap::new(), sessions: HashMap::new(),
@ -108,14 +108,14 @@ impl Handler<Connect> for ChatServer {
println!("Someone joined"); println!("Someone joined");
// notify all users in same room // notify all users in same room
self.send_message("Main", "Someone joined", 0); self.send_message("main", "Someone joined", 0);
// register session with random id // register session with random id
let id = self.rng.gen::<usize>(); let id = self.rng.gen::<usize>();
self.sessions.insert(id, msg.addr); self.sessions.insert(id, msg.addr);
// auto join session to Main room // auto join session to main room
self.rooms.get_mut(&"Main".to_owned()).unwrap().insert(id); self.rooms.get_mut("main").unwrap().insert(id);
// send id back // send id back
id id

View File

@ -147,7 +147,7 @@ impl ChatSession {
id: 0, id: 0,
addr, addr,
hb: Instant::now(), hb: Instant::now(),
room: "Main".to_owned(), room: "main".to_owned(),
framed, framed,
} }
} }

View File

@ -30,7 +30,7 @@ async fn chat_route(
session::WsChatSession { session::WsChatSession {
id: 0, id: 0,
hb: Instant::now(), hb: Instant::now(),
room: "Main".to_owned(), room: "main".to_owned(),
name: None, name: None,
addr: srv.get_ref().clone(), addr: srv.get_ref().clone(),
}, },

View File

@ -79,7 +79,7 @@ impl ChatServer {
pub fn new(visitor_count: Arc<AtomicUsize>) -> ChatServer { pub fn new(visitor_count: Arc<AtomicUsize>) -> ChatServer {
// default room // default room
let mut rooms = HashMap::new(); let mut rooms = HashMap::new();
rooms.insert("Main".to_owned(), HashSet::new()); rooms.insert("main".to_owned(), HashSet::new());
ChatServer { ChatServer {
sessions: HashMap::new(), sessions: HashMap::new(),
@ -122,20 +122,20 @@ impl Handler<Connect> for ChatServer {
println!("Someone joined"); println!("Someone joined");
// notify all users in same room // notify all users in same room
self.send_message("Main", "Someone joined", 0); self.send_message("main", "Someone joined", 0);
// register session with random id // register session with random id
let id = self.rng.gen::<usize>(); let id = self.rng.gen::<usize>();
self.sessions.insert(id, msg.addr); self.sessions.insert(id, msg.addr);
// auto join session to Main room // auto join session to main room
self.rooms self.rooms
.entry("Main".to_owned()) .entry("main".to_owned())
.or_insert_with(HashSet::new) .or_insert_with(HashSet::new)
.insert(id); .insert(id);
let count = self.visitor_count.fetch_add(1, Ordering::SeqCst); let count = self.visitor_count.fetch_add(1, Ordering::SeqCst);
self.send_message("Main", &format!("Total visitors {count}"), 0); self.send_message("main", &format!("Total visitors {count}"), 0);
// send id back // send id back
id id

View File

@ -11,6 +11,6 @@ awc = "3"
env_logger = "0.9" env_logger = "0.9"
futures-util = { version = "0.3.7", default-features = false, features = ["std", "sink"] } futures-util = { version = "0.3.7", default-features = false, features = ["std", "sink"] }
futures-lite = "1.3"
log = "0.4" log = "0.4"
tokio = { version = "1.13.1", features = ["full"] } tokio = { version = "1.13.1", features = ["full"] }
tokio-stream = "0.1.8"

View File

@ -12,9 +12,9 @@ cargo run
# starting HTTP server at http://localhost:8080 # starting HTTP server at http://localhost:8080
``` ```
### Web Client ### Browser Client
Go to <http://localhost:8080/> in a browser. Go to <http://localhost:8080> in a browser.
### CLI Client ### CLI Client

View File

@ -2,8 +2,8 @@ use std::time::{Duration, Instant};
use actix_web::rt; use actix_web::rt;
use actix_ws::Message; use actix_ws::Message;
use futures_util::stream::StreamExt as _; use futures_lite::future;
use tokio::select; use futures_util::{future::Either, stream::StreamExt as _, FutureExt as _};
/// How often heartbeat pings are sent /// How often heartbeat pings are sent
const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5); const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5);
@ -20,12 +20,16 @@ pub async fn echo_heartbeat_ws(
log::info!("connected"); log::info!("connected");
let mut last_heartbeat = Instant::now(); let mut last_heartbeat = Instant::now();
let mut interval = rt::time::interval(HEARTBEAT_INTERVAL); let mut interval = rt::time::interval(HEARTBEAT_INTERVAL);
loop { loop {
select! { match future::or(
Some(Ok(msg)) = msg_stream.next() => { msg_stream.next().map(Either::Left),
interval.tick().map(Either::Right),
)
.await
{
Either::Left(Some(Ok(msg))) => {
log::debug!("msg: {msg:?}"); log::debug!("msg: {msg:?}");
match msg { match msg {
@ -58,10 +62,14 @@ pub async fn echo_heartbeat_ws(
} }
} }
_ = interval.tick() => { Either::Left(_) => {}
Either::Right(_) => {
// if no heartbeat ping/pong received recently, close the connection // if no heartbeat ping/pong received recently, close the connection
if Instant::now().duration_since(last_heartbeat) > CLIENT_TIMEOUT { if Instant::now().duration_since(last_heartbeat) > CLIENT_TIMEOUT {
log::info!("client has not sent heartbeat in over {CLIENT_TIMEOUT:?}; disconnecting"); log::info!(
"client has not sent heartbeat in over {CLIENT_TIMEOUT:?}; disconnecting"
);
let _ = session.close(None).await; let _ = session.close(None).await;
break; break;
} }