Commit e6b0f4c6 by qlintonger xeno

CmdUpdateOnlineUsers完毕!

parent 9302eff9
/target
.idea
Cargo.lock
.venv
test
\ No newline at end of file
......@@ -16,6 +16,7 @@ use tokio::time;
use tokio_tungstenite::accept_hdr_async;
use tungstenite::handshake::server::{Request, Response};
use tungstenite::{Error, Message};
use crate::handles::online_users_update::send_online_users_resp;
lazy_static! {
pub static ref CONNECTIONS: ConnectionMap = Arc::new(AsyncRwLock::new(HashMap::new()));
......@@ -111,6 +112,13 @@ pub(crate) async fn handle_client(stream: tokio::net::TcpStream) -> Result<(), E
println!("将用户信息插入到 Redis 中时出错: {}", e);
}
// 准备更新用户链接
if let Err(e) = send_online_users_resp().await {
println!("在处理新用户id {} 之时广播,处理在线用户列表出错了:{:?}", &from_id,e);
} else {
println!("广播消息 来源id {} 成功", &from_id);
}
let task = tokio::spawn(async move {
let mut last_heartbeat_time = Instant::now();
loop {
......@@ -142,6 +150,14 @@ pub(crate) async fn handle_client(stream: tokio::net::TcpStream) -> Result<(), E
}
}
},
"GetOnlineUserList" => {
println!("收到客户端获取在线用户列表 {:?}", &data);
if let Err(e) = send_online_users_resp().await {
println!("处理在线用户列表出错了:{:?}", e);
handle_connection_error(&from_id_clone, &CONNECTIONS, &TASKS).await;
break;
}
},
_ => {
if let Err(e) = handle_other_message(&mut sender_ref, &data, &from_id_clone).await {
println!("Failed to handle other message: {}", e);
......
use crate::handles::redis::remove_this_connection;
use crate::typing::used_typed::{ConnectionMap, TaskMap};
use tokio::sync::RwLockWriteGuard;
use crate::handles::online_users_update::send_online_users_resp;
pub async fn handle_connection_error(
from_id: &str,
......@@ -43,4 +44,11 @@ pub async fn handle_connection_error(
}
println!("断开与用户id: {} 的连接并完成清理操作", from_id);
// 准备更新用户链接
if let Err(e) = send_online_users_resp().await {
println!("在处理新用户id {} 之时广播,处理在线用户列表出错了:{:?}", &from_id,e);
} else {
println!("成功将用户id {} 退出广播至其余用户", &from_id);
}
}
\ No newline at end of file
......@@ -60,10 +60,11 @@ where
Ok(Arc::new(data))
}
pub async fn send_multiple_messages() -> Result<(), Error> {
pub async fn send_online_users_resp() -> Result<(), Error> {
let mut msg_data = Vec::new();
{
let online_users = ONLINE_USERS.read().await;
println!("当前所有用户数据信息 ONLINE_USERS: {:?}", online_users);
for (_, user_info_str) in online_users.iter() {
let parts: Vec<&str> = user_info_str.split(',').collect();
if parts.len() == 9 {
......@@ -88,7 +89,7 @@ pub async fn send_multiple_messages() -> Result<(), Error> {
for (_, connection) in connections.iter_mut() {
let to_id = connection.from_id.to_string();
let message = OnlineUsersMessage {
msg_type: "GetOnlineUserList".to_string(),
msg_type: "CmdUpdateOnlineUsers".to_string(),
from_id: "0".to_string(),
from_name: "Server".to_string(),
to_id,
......
......@@ -48,9 +48,10 @@ pub async fn insert_this_connection(
// 按照结构体OnlineUserMessage的格式构造用户信息字符串,用逗号拼接即可
// callState,channelID,deviceID,fromID,hasCamera,hasMike,isHost,userCallGroup,fromName
let user_info_str = format!(
"{},{},{},1,1,0,0,{}",
"{},{},{},{},1,1,0,0,{}",
"idle",
"",
from_id,
device_id,
from_name
);
......@@ -67,7 +68,7 @@ pub async fn insert_this_connection(
// 获取写锁以进行写操作
let mut online_users = ONLINE_USERS.write().await;
online_users.insert(from_id.to_string(), user_info_str);
println!("成功将用户id: {} 的信息插入到 Redis 中", from_id);
println!("成功将用户id: {} 的信息插入到 ONLINE_USERS 中", from_id);
}
Ok(())
}
\ 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