1
0
mirror of https://github.com/actix/examples synced 2024-11-27 16:02:57 +01:00

refactor: replace raw types with aliases from main

This commit is contained in:
One 2024-07-21 14:10:09 -04:00
parent f52f7785fe
commit 65acc77431

View File

@ -87,7 +87,7 @@ impl ChatServer {
/// Send message to users in a room. /// Send message to users in a room.
/// ///
/// `skip` is used to prevent messages triggered by a connection also being received by it. /// `skip` is used to prevent messages triggered by a connection also being received by it.
async fn send_system_message(&self, room: &str, skip: ConnId, msg: impl Into<String>) { async fn send_system_message(&self, room: &str, skip: ConnId, msg: impl Into<Msg>) {
if let Some(sessions) = self.rooms.get(room) { if let Some(sessions) = self.rooms.get(room) {
let msg = msg.into(); let msg = msg.into();
@ -106,7 +106,7 @@ impl ChatServer {
/// ///
/// `conn` is used to find current room and prevent messages sent by a connection also being /// `conn` is used to find current room and prevent messages sent by a connection also being
/// received by it. /// received by it.
async fn send_message(&self, conn: ConnId, msg: impl Into<String>) { async fn send_message(&self, conn: ConnId, msg: impl Into<Msg>) {
if let Some(room) = self if let Some(room) = self
.rooms .rooms
.iter() .iter()
@ -124,7 +124,7 @@ impl ChatServer {
self.send_system_message("main", 0, "Someone joined").await; self.send_system_message("main", 0, "Someone joined").await;
// register session with random connection ID // register session with random connection ID
let id = thread_rng().gen::<usize>(); let id = thread_rng().gen::<ConnId>();
self.sessions.insert(id, tx); self.sessions.insert(id, tx);
// auto join session to main room // auto join session to main room
@ -142,7 +142,7 @@ impl ChatServer {
async fn disconnect(&mut self, conn_id: ConnId) { async fn disconnect(&mut self, conn_id: ConnId) {
println!("Someone disconnected"); println!("Someone disconnected");
let mut rooms: Vec<String> = Vec::new(); let mut rooms: Vec<RoomId> = Vec::new();
// remove sender // remove sender
if self.sessions.remove(&conn_id).is_some() { if self.sessions.remove(&conn_id).is_some() {
@ -162,12 +162,12 @@ impl ChatServer {
} }
/// Returns list of created room names. /// Returns list of created room names.
fn list_rooms(&mut self) -> Vec<String> { fn list_rooms(&mut self) -> Vec<RoomId> {
self.rooms.keys().cloned().collect() self.rooms.keys().cloned().collect()
} }
/// Join room, send disconnect message to old room send join message to new room. /// Join room, send disconnect message to old room send join message to new room.
async fn join_room(&mut self, conn_id: ConnId, room: String) { async fn join_room(&mut self, conn_id: ConnId, room: RoomId) {
let mut rooms = Vec::new(); let mut rooms = Vec::new();
// remove session from all rooms // remove session from all rooms
@ -230,7 +230,7 @@ pub struct ChatServerHandle {
impl ChatServerHandle { impl ChatServerHandle {
/// Register client message sender and obtain connection ID. /// Register client message sender and obtain connection ID.
pub async fn connect(&self, conn_tx: mpsc::UnboundedSender<String>) -> ConnId { pub async fn connect(&self, conn_tx: mpsc::UnboundedSender<Msg>) -> ConnId {
let (res_tx, res_rx) = oneshot::channel(); let (res_tx, res_rx) = oneshot::channel();
// unwrap: chat server should not have been dropped // unwrap: chat server should not have been dropped
@ -243,7 +243,7 @@ impl ChatServerHandle {
} }
/// List all created rooms. /// List all created rooms.
pub async fn list_rooms(&self) -> Vec<String> { pub async fn list_rooms(&self) -> Vec<RoomId> {
let (res_tx, res_rx) = oneshot::channel(); let (res_tx, res_rx) = oneshot::channel();
// unwrap: chat server should not have been dropped // unwrap: chat server should not have been dropped
@ -254,7 +254,7 @@ impl ChatServerHandle {
} }
/// Join `room`, creating it if it does not exist. /// Join `room`, creating it if it does not exist.
pub async fn join_room(&self, conn: ConnId, room: impl Into<String>) { pub async fn join_room(&self, conn: ConnId, room: impl Into<RoomId>) {
let (res_tx, res_rx) = oneshot::channel(); let (res_tx, res_rx) = oneshot::channel();
// unwrap: chat server should not have been dropped // unwrap: chat server should not have been dropped
@ -271,7 +271,7 @@ impl ChatServerHandle {
} }
/// Broadcast message to current room. /// Broadcast message to current room.
pub async fn send_message(&self, conn: ConnId, msg: impl Into<String>) { pub async fn send_message(&self, conn: ConnId, msg: impl Into<Msg>) {
let (res_tx, res_rx) = oneshot::channel(); let (res_tx, res_rx) = oneshot::channel();
// unwrap: chat server should not have been dropped // unwrap: chat server should not have been dropped