Commit b6126d1e by qlintonger xeno

尝试使用事件机制来实现多线程沟通

parent 683aaa2a
use crate::handles::online_users_update::send_online_users_resp;
use tokio::sync::mpsc;
// 假设的用户详细信息结构体
#[derive(Debug)]
pub struct UserDetails {
pub username: String,
}
// 定义事件类型
#[derive(Debug)]
pub enum Event {
NewConnection(String),
CloseConnection(String),
UpdateOnlineUsers,
}
// 处理事件的任务
pub async fn handle_events(mut receiver: mpsc::UnboundedReceiver<Event>) {
while let Some(event) = receiver.recv().await {
match event {
Event::NewConnection(from_id) => {
println!("新连接: {}", from_id);
// 这里假设在其他地方有获取所有连接发送器的逻辑,暂时省略
// 实际应用中,需要实现向所有连接发送消息的功能
if let Err(e) = send_online_users_resp().await {
println!(
"在处理新用户id {} 之时广播,处理在线用户列表出错了:{:?}",
&from_id, e
);
}
}
Event::CloseConnection(from_id) => {
println!("关闭连接: {}", from_id);
if let Err(e) = send_online_users_resp().await {
println!(
"在处理新用户id {} 之时广播,处理在线用户列表出错了:{:?}",
&from_id, e
);
}
}
Event::UpdateOnlineUsers => {
// 这里可以实现其他触发更新在线用户列表的逻辑
// 为简单起见,暂未详细实现
println!("更新在线用户列表事件触发");
}
}
}
}
use crate::handles::online_users_update::send_online_users_resp; use crate::handles::online_users_update::send_online_users_resp;
use crate::handles::redis::remove_this_connection; use crate::handles::redis::remove_this_connection;
use crate::typing::used_typed::{ConnectionMap, TaskMap}; use crate::typing::used_typed::{ConnectionMap};
use tokio::sync::RwLockWriteGuard; use tokio::sync::RwLockWriteGuard;
pub async fn handle_connection_error( pub async fn handle_connection_error(
from_id: &str, from_id: &str,
connections: &ConnectionMap, connections: &ConnectionMap,
tasks: &TaskMap,
) { ) {
println!("开始处理用户id: {} 的连接错误", from_id); println!("开始处理用户id: {} 的连接错误", from_id);
// 从全局连接映射中移除该连接 // 从全局连接映射中移除该连接
...@@ -30,22 +29,6 @@ pub async fn handle_connection_error( ...@@ -30,22 +29,6 @@ pub async fn handle_connection_error(
} }
} }
println!("开始尝试关闭用户id: {} 的 redis 连接", from_id);
// 取消对应的任务
{
let mut tasks = tasks.write().unwrap();
match tasks.remove(from_id) {
Some(task) => {
task.abort();
println!("成功取消用户id: {} 的任务", from_id);
}
None => println!("未在全局任务映射中找到用户id: {} 的任务", from_id),
}
}
println!("断开与用户id: {} 的连接并完成清理操作", from_id);
// 准备更新用户链接 // 准备更新用户链接
if let Err(e) = send_online_users_resp().await { if let Err(e) = send_online_users_resp().await {
println!("在处理新用户id {} 之时广播,处理在线用户列表出错了:{:?}", &from_id,e); println!("在处理新用户id {} 之时广播,处理在线用户列表出错了:{:?}", &from_id,e);
......
...@@ -5,15 +5,22 @@ mod config; ...@@ -5,15 +5,22 @@ mod config;
mod handles; mod handles;
mod typing; mod typing;
mod utils; mod utils;
mod events;
use crate::events::handle_events;
use client::handle_client; use client::handle_client;
use config::config::STATIC_ADDR as addr; use config::config::STATIC_ADDR as addr;
use tokio::net::TcpListener; use tokio::net::TcpListener;
use tokio::sync::mpsc;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let listener = TcpListener::bind(addr).await.unwrap(); let listener = TcpListener::bind(addr).await.unwrap();
// 创建事件通道
let (event_sender, event_receiver) = mpsc::unbounded_channel();
// 启动事件处理任务
tokio::spawn(handle_events(event_receiver));
while let Ok((stream, _)) = listener.accept().await { while let Ok((stream, _)) = listener.accept().await {
tokio::spawn(handle_client(stream)); tokio::spawn(handle_client(stream, event_sender.clone()));
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment