mirror of
https://github.com/actix/examples
synced 2024-11-23 14:31:07 +01:00
remove select macro from echo example
This commit is contained in:
parent
a4a060994d
commit
fd17252725
@ -78,7 +78,7 @@ impl Actor for WsChatSession {
|
||||
type Context = ws::WebsocketContext<Self>;
|
||||
|
||||
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) {
|
||||
|
@ -29,7 +29,7 @@ async fn chat_route(
|
||||
WsChatSession {
|
||||
id: 0,
|
||||
hb: Instant::now(),
|
||||
room: "Main".to_owned(),
|
||||
room: "main".to_owned(),
|
||||
name: None,
|
||||
addr: srv.get_ref().clone(),
|
||||
},
|
||||
|
@ -66,7 +66,7 @@ impl Default for ChatServer {
|
||||
fn default() -> ChatServer {
|
||||
// default room
|
||||
let mut rooms = HashMap::new();
|
||||
rooms.insert("Main".to_owned(), HashSet::new());
|
||||
rooms.insert("main".to_owned(), HashSet::new());
|
||||
|
||||
ChatServer {
|
||||
sessions: HashMap::new(),
|
||||
@ -108,14 +108,14 @@ impl Handler<Connect> for ChatServer {
|
||||
println!("Someone joined");
|
||||
|
||||
// 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
|
||||
let id = self.rng.gen::<usize>();
|
||||
self.sessions.insert(id, msg.addr);
|
||||
|
||||
// auto join session to Main room
|
||||
self.rooms.get_mut(&"Main".to_owned()).unwrap().insert(id);
|
||||
// auto join session to main room
|
||||
self.rooms.get_mut("main").unwrap().insert(id);
|
||||
|
||||
// send id back
|
||||
id
|
||||
|
@ -147,7 +147,7 @@ impl ChatSession {
|
||||
id: 0,
|
||||
addr,
|
||||
hb: Instant::now(),
|
||||
room: "Main".to_owned(),
|
||||
room: "main".to_owned(),
|
||||
framed,
|
||||
}
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ async fn chat_route(
|
||||
session::WsChatSession {
|
||||
id: 0,
|
||||
hb: Instant::now(),
|
||||
room: "Main".to_owned(),
|
||||
room: "main".to_owned(),
|
||||
name: None,
|
||||
addr: srv.get_ref().clone(),
|
||||
},
|
||||
|
@ -79,7 +79,7 @@ impl ChatServer {
|
||||
pub fn new(visitor_count: Arc<AtomicUsize>) -> ChatServer {
|
||||
// default room
|
||||
let mut rooms = HashMap::new();
|
||||
rooms.insert("Main".to_owned(), HashSet::new());
|
||||
rooms.insert("main".to_owned(), HashSet::new());
|
||||
|
||||
ChatServer {
|
||||
sessions: HashMap::new(),
|
||||
@ -122,20 +122,20 @@ impl Handler<Connect> for ChatServer {
|
||||
println!("Someone joined");
|
||||
|
||||
// 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
|
||||
let id = self.rng.gen::<usize>();
|
||||
self.sessions.insert(id, msg.addr);
|
||||
|
||||
// auto join session to Main room
|
||||
// auto join session to main room
|
||||
self.rooms
|
||||
.entry("Main".to_owned())
|
||||
.entry("main".to_owned())
|
||||
.or_insert_with(HashSet::new)
|
||||
.insert(id);
|
||||
|
||||
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
|
||||
id
|
||||
|
@ -11,6 +11,6 @@ awc = "3"
|
||||
|
||||
env_logger = "0.9"
|
||||
futures-util = { version = "0.3.7", default-features = false, features = ["std", "sink"] }
|
||||
futures-lite = "1.3"
|
||||
log = "0.4"
|
||||
tokio = { version = "1.13.1", features = ["full"] }
|
||||
tokio-stream = "0.1.8"
|
||||
|
@ -12,9 +12,9 @@ cargo run
|
||||
# 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
|
||||
|
||||
|
@ -2,8 +2,8 @@ use std::time::{Duration, Instant};
|
||||
|
||||
use actix_web::rt;
|
||||
use actix_ws::Message;
|
||||
use futures_util::stream::StreamExt as _;
|
||||
use tokio::select;
|
||||
use futures_lite::future;
|
||||
use futures_util::{future::Either, stream::StreamExt as _, FutureExt as _};
|
||||
|
||||
/// How often heartbeat pings are sent
|
||||
const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5);
|
||||
@ -20,12 +20,16 @@ pub async fn echo_heartbeat_ws(
|
||||
log::info!("connected");
|
||||
|
||||
let mut last_heartbeat = Instant::now();
|
||||
|
||||
let mut interval = rt::time::interval(HEARTBEAT_INTERVAL);
|
||||
|
||||
loop {
|
||||
select! {
|
||||
Some(Ok(msg)) = msg_stream.next() => {
|
||||
match future::or(
|
||||
msg_stream.next().map(Either::Left),
|
||||
interval.tick().map(Either::Right),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Either::Left(Some(Ok(msg))) => {
|
||||
log::debug!("msg: {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 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;
|
||||
break;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user