mirror of
https://github.com/actix/examples
synced 2025-02-25 18:42:50 +01:00
chore: update deps
This commit is contained in:
parent
ad0c1b765e
commit
a6df6cc903
877
Cargo.lock
generated
877
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -17,7 +17,7 @@ mod server;
|
||||
pub use self::server::{ChatServer, ChatServerHandle};
|
||||
|
||||
/// Connection ID.
|
||||
pub type ConnId = usize;
|
||||
pub type ConnId = u64;
|
||||
|
||||
/// Room ID.
|
||||
pub type RoomId = String;
|
||||
|
@ -9,7 +9,7 @@ use std::{
|
||||
},
|
||||
};
|
||||
|
||||
use rand::{thread_rng, Rng as _};
|
||||
use rand::Rng as _;
|
||||
use tokio::sync::{mpsc, oneshot};
|
||||
|
||||
use crate::{ConnId, Msg, RoomId};
|
||||
@ -124,7 +124,7 @@ impl ChatServer {
|
||||
self.send_system_message("main", 0, "Someone joined").await;
|
||||
|
||||
// register session with random connection ID
|
||||
let id = thread_rng().gen::<ConnId>();
|
||||
let id = rand::rng().random::<ConnId>();
|
||||
self.sessions.insert(id, tx);
|
||||
|
||||
// auto join session to main room
|
||||
|
@ -59,7 +59,6 @@ pub struct Join {
|
||||
pub struct ChatServer {
|
||||
sessions: HashMap<usize, Recipient<session::Message>>,
|
||||
rooms: HashMap<String, HashSet<usize>>,
|
||||
rng: ThreadRng,
|
||||
}
|
||||
|
||||
impl Default for ChatServer {
|
||||
@ -71,7 +70,6 @@ impl Default for ChatServer {
|
||||
ChatServer {
|
||||
sessions: HashMap::new(),
|
||||
rooms,
|
||||
rng: rand::thread_rng(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -111,7 +109,7 @@ impl Handler<Connect> for ChatServer {
|
||||
self.send_message("main", "Someone joined", 0);
|
||||
|
||||
// register session with random id
|
||||
let id = self.rng.gen::<usize>();
|
||||
let id = rand::rng().random::<usize>();
|
||||
self.sessions.insert(id, msg.addr);
|
||||
|
||||
// auto join session to main room
|
||||
|
@ -11,7 +11,7 @@ use std::{
|
||||
};
|
||||
|
||||
use actix::prelude::*;
|
||||
use rand::{rngs::ThreadRng, Rng};
|
||||
use rand::Rng as _;
|
||||
|
||||
/// Chat server sends this messages to session
|
||||
#[derive(Message)]
|
||||
@ -22,7 +22,7 @@ pub struct Message(pub String);
|
||||
///
|
||||
/// New chat session is created
|
||||
#[derive(Message)]
|
||||
#[rtype(usize)]
|
||||
#[rtype(u64)]
|
||||
pub struct Connect {
|
||||
pub addr: Recipient<Message>,
|
||||
}
|
||||
@ -31,7 +31,7 @@ pub struct Connect {
|
||||
#[derive(Message)]
|
||||
#[rtype(result = "()")]
|
||||
pub struct Disconnect {
|
||||
pub id: usize,
|
||||
pub id: u64,
|
||||
}
|
||||
|
||||
/// Send message to specific room
|
||||
@ -39,7 +39,7 @@ pub struct Disconnect {
|
||||
#[rtype(result = "()")]
|
||||
pub struct ClientMessage {
|
||||
/// Id of the client session
|
||||
pub id: usize,
|
||||
pub id: u64,
|
||||
/// Peer message
|
||||
pub msg: String,
|
||||
/// Room name
|
||||
@ -58,7 +58,7 @@ impl actix::Message for ListRooms {
|
||||
#[rtype(result = "()")]
|
||||
pub struct Join {
|
||||
/// Client ID
|
||||
pub id: usize,
|
||||
pub id: u64,
|
||||
|
||||
/// Room name
|
||||
pub name: String,
|
||||
@ -69,9 +69,8 @@ pub struct Join {
|
||||
/// Implementation is very naïve.
|
||||
#[derive(Debug)]
|
||||
pub struct ChatServer {
|
||||
sessions: HashMap<usize, Recipient<Message>>,
|
||||
rooms: HashMap<String, HashSet<usize>>,
|
||||
rng: ThreadRng,
|
||||
sessions: HashMap<u64, Recipient<Message>>,
|
||||
rooms: HashMap<String, HashSet<u64>>,
|
||||
visitor_count: Arc<AtomicUsize>,
|
||||
}
|
||||
|
||||
@ -84,7 +83,6 @@ impl ChatServer {
|
||||
ChatServer {
|
||||
sessions: HashMap::new(),
|
||||
rooms,
|
||||
rng: rand::thread_rng(),
|
||||
visitor_count,
|
||||
}
|
||||
}
|
||||
@ -92,7 +90,7 @@ impl ChatServer {
|
||||
|
||||
impl ChatServer {
|
||||
/// Send message to all users in the room
|
||||
fn send_message(&self, room: &str, message: &str, skip_id: usize) {
|
||||
fn send_message(&self, room: &str, message: &str, skip_id: u64) {
|
||||
if let Some(sessions) = self.rooms.get(room) {
|
||||
for id in sessions {
|
||||
if *id != skip_id {
|
||||
@ -116,7 +114,7 @@ impl Actor for ChatServer {
|
||||
///
|
||||
/// Register new session and assign unique id to this session
|
||||
impl Handler<Connect> for ChatServer {
|
||||
type Result = usize;
|
||||
type Result = u64;
|
||||
|
||||
fn handle(&mut self, msg: Connect, _: &mut Context<Self>) -> Self::Result {
|
||||
println!("Someone joined");
|
||||
@ -125,7 +123,7 @@ impl Handler<Connect> for ChatServer {
|
||||
self.send_message("main", "Someone joined", 0);
|
||||
|
||||
// register session with random id
|
||||
let id = self.rng.gen::<usize>();
|
||||
let id = rand::rng().random::<u64>();
|
||||
self.sessions.insert(id, msg.addr);
|
||||
|
||||
// auto join session to main room
|
||||
|
@ -14,7 +14,7 @@ const CLIENT_TIMEOUT: Duration = Duration::from_secs(10);
|
||||
#[derive(Debug)]
|
||||
pub struct WsChatSession {
|
||||
/// unique session id
|
||||
pub id: usize,
|
||||
pub id: u64,
|
||||
|
||||
/// Client must send ping at least once per 10 seconds (CLIENT_TIMEOUT),
|
||||
/// otherwise we drop connection.
|
||||
|
Loading…
x
Reference in New Issue
Block a user