mirror of
https://github.com/actix/examples
synced 2025-06-26 17:17:42 +02:00
chore: update derive_more to v2
This commit is contained in:
@ -5,12 +5,12 @@ use actix::prelude::*;
|
||||
pub struct ChatMessage(pub String);
|
||||
|
||||
#[derive(Clone, Message)]
|
||||
#[rtype(result = "usize")]
|
||||
#[rtype(result = "u64")]
|
||||
pub struct JoinRoom(pub String, pub Option<String>, pub Recipient<ChatMessage>);
|
||||
|
||||
#[derive(Clone, Message)]
|
||||
#[rtype(result = "()")]
|
||||
pub struct LeaveRoom(pub String, pub usize);
|
||||
pub struct LeaveRoom(pub String, pub u64);
|
||||
|
||||
#[derive(Clone, Message)]
|
||||
#[rtype(result = "Vec<String>")]
|
||||
@ -18,4 +18,4 @@ pub struct ListRooms;
|
||||
|
||||
#[derive(Clone, Message)]
|
||||
#[rtype(result = "()")]
|
||||
pub struct SendMessage(pub String, pub usize, pub String);
|
||||
pub struct SendMessage(pub String, pub u64, pub String);
|
||||
|
@ -6,7 +6,7 @@ use actix_broker::BrokerSubscribe;
|
||||
use crate::message::{ChatMessage, JoinRoom, LeaveRoom, ListRooms, SendMessage};
|
||||
|
||||
type Client = Recipient<ChatMessage>;
|
||||
type Room = HashMap<usize, Client>;
|
||||
type Room = HashMap<u64, Client>;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct WsChatServer {
|
||||
@ -20,13 +20,13 @@ impl WsChatServer {
|
||||
Some(room)
|
||||
}
|
||||
|
||||
fn add_client_to_room(&mut self, room_name: &str, id: Option<usize>, client: Client) -> usize {
|
||||
let mut id = id.unwrap_or_else(rand::random::<usize>);
|
||||
fn add_client_to_room(&mut self, room_name: &str, id: Option<u64>, client: Client) -> u64 {
|
||||
let mut id = id.unwrap_or_else(rand::random);
|
||||
|
||||
if let Some(room) = self.rooms.get_mut(room_name) {
|
||||
loop {
|
||||
if room.contains_key(&id) {
|
||||
id = rand::random::<usize>();
|
||||
id = rand::random();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
@ -45,7 +45,7 @@ impl WsChatServer {
|
||||
id
|
||||
}
|
||||
|
||||
fn send_chat_message(&mut self, room_name: &str, msg: &str, _src: usize) -> Option<()> {
|
||||
fn send_chat_message(&mut self, room_name: &str, msg: &str, _src: u64) -> Option<()> {
|
||||
let mut room = self.take_room(room_name)?;
|
||||
|
||||
for (id, client) in room.drain() {
|
||||
|
@ -9,7 +9,7 @@ use crate::{
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct WsChatSession {
|
||||
id: usize,
|
||||
id: u64,
|
||||
room: String,
|
||||
name: Option<String>,
|
||||
}
|
||||
|
@ -40,7 +40,7 @@ async fn chat_route(
|
||||
|
||||
struct WsChatSession {
|
||||
/// unique session id
|
||||
id: usize,
|
||||
id: u64,
|
||||
/// Client must send ping at least once per 10 seconds (CLIENT_TIMEOUT),
|
||||
/// otherwise we drop connection.
|
||||
hb: Instant,
|
||||
|
@ -5,7 +5,7 @@
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
use actix::prelude::*;
|
||||
use rand::{rngs::ThreadRng, Rng};
|
||||
use rand::Rng as _;
|
||||
|
||||
use crate::session;
|
||||
|
||||
@ -13,7 +13,7 @@ use crate::session;
|
||||
///
|
||||
/// New chat session is created
|
||||
#[derive(Message)]
|
||||
#[rtype(usize)]
|
||||
#[rtype(u64)]
|
||||
pub struct Connect {
|
||||
pub addr: Recipient<session::Message>,
|
||||
}
|
||||
@ -22,7 +22,7 @@ pub struct Connect {
|
||||
#[derive(Message)]
|
||||
#[rtype(result = "()")]
|
||||
pub struct Disconnect {
|
||||
pub id: usize,
|
||||
pub id: u64,
|
||||
}
|
||||
|
||||
/// Send message to specific room
|
||||
@ -30,7 +30,7 @@ pub struct Disconnect {
|
||||
#[rtype(result = "()")]
|
||||
pub struct Message {
|
||||
/// Id of the client session
|
||||
pub id: usize,
|
||||
pub id: u64,
|
||||
/// Peer message
|
||||
pub msg: String,
|
||||
/// Room name
|
||||
@ -49,7 +49,7 @@ impl actix::Message for ListRooms {
|
||||
#[rtype(result = "()")]
|
||||
pub struct Join {
|
||||
/// Client id
|
||||
pub id: usize,
|
||||
pub id: u64,
|
||||
/// Room name
|
||||
pub name: String,
|
||||
}
|
||||
@ -57,8 +57,8 @@ pub struct Join {
|
||||
/// `ChatServer` manages chat rooms and responsible for coordinating chat
|
||||
/// session. implementation is super primitive
|
||||
pub struct ChatServer {
|
||||
sessions: HashMap<usize, Recipient<session::Message>>,
|
||||
rooms: HashMap<String, HashSet<usize>>,
|
||||
sessions: HashMap<u64, Recipient<session::Message>>,
|
||||
rooms: HashMap<String, HashSet<u64>>,
|
||||
}
|
||||
|
||||
impl Default for ChatServer {
|
||||
@ -76,7 +76,7 @@ impl Default for 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 {
|
||||
@ -100,7 +100,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");
|
||||
@ -109,7 +109,7 @@ impl Handler<Connect> for ChatServer {
|
||||
self.send_message("main", "Someone joined", 0);
|
||||
|
||||
// register session with random id
|
||||
let id = rand::rng().random::<usize>();
|
||||
let id = rand::rng().random::<u64>();
|
||||
self.sessions.insert(id, msg.addr);
|
||||
|
||||
// auto join session to main room
|
||||
|
@ -27,7 +27,7 @@ pub struct Message(pub String);
|
||||
/// `ChatSession` actor is responsible for tcp peer communications.
|
||||
pub struct ChatSession {
|
||||
/// unique session id
|
||||
id: usize,
|
||||
id: u64,
|
||||
/// this is address of chat server
|
||||
addr: Addr<ChatServer>,
|
||||
/// Client must send ping at least once per 10 seconds, otherwise we drop
|
||||
|
Reference in New Issue
Block a user