Commit 7fbb3b4f by qlintonger xeno

阐释添加额外消息发送

parent f1a8eadf
use crate::events::Event;
use crate::events::{Event, CLIENT_SENDERS};
use crate::handles::handle_agora_call::handle_agora_call;
use crate::handles::online_users_update::{online_messages, send_online_users_resp};
use crate::typing::message_typed::ClientMessageData;
use tokio::sync::mpsc::UnboundedSender;
use crate::client::ONLINE_USERS;
pub async fn handle_ws_msg(
client_message_data: &ClientMessageData,
......@@ -44,6 +45,79 @@ pub async fn handle_ws_msg(
.await;
}
// 针对其余消息类型,直接根据fromID -> toID规则发送即可
_ => {}
_ => {
let to_id = &client_message_data.to_id;
let msg_json = serde_json::json!({
"msgType": msg_type,
"msgData": client_message_data.msg_data,
"fromID": from_id,
"fromName": client_message_data.from_name,
"toID": to_id
}).to_string();
println!("收到客户端消息 类型: {} 来自: {} 发送给: {}", msg_type, from_id, to_id);
if to_id == "-2" {
// 广播消息给同频道的所有在线用户
if let Some(user_data) = ONLINE_USERS.get(&from_id) {
let channel_id = user_data.split(',').nth(1).unwrap_or("").to_string();
println!("准备广播消息到频道: {} 来自用户: {}", channel_id, from_id);
tokio::spawn({
let event_sender = event_sender.clone();
let msg_json = msg_json.clone();
let from_id = from_id.clone();
async move {
for entry in ONLINE_USERS.iter() {
let target_id = entry.key();
let target_data = entry.value();
if target_data.split(',').nth(1).unwrap_or("") == channel_id
&& target_id != &from_id {
if let Some(sender) = CLIENT_SENDERS.iter()
.find(|e| e.key().0 == *target_id)
.map(|e| e.key().clone()) {
println!("发送广播消息给用户: {} 内容: {}", target_id, msg_json);
if let Err(e) = event_sender.send(Event::SendClientMessage(
sender,
msg_json.clone(),
false,
)) {
println!("发送广播消息给用户: {} 失败: {:?}", target_id, e);
} else {
println!("发送广播消息给用户: {} 成功", target_id);
}
} else {
println!("未找到用户: {} 的sender,无法发送广播消息", target_id);
}
}
}
}
});
} else {
println!("用户: {} 不在线或无频道信息,无法广播", from_id);
}
} else {
// 单播消息给指定用户
if ONLINE_USERS.contains_key(to_id) {
if let Some(sender) = CLIENT_SENDERS.iter()
.find(|e| e.key().0 == *to_id)
.map(|e| e.key().clone()) {
println!("发送单播消息给用户: {} 内容: {}", to_id, msg_json);
if let Err(e) = event_sender.send(Event::SendClientMessage(
sender,
msg_json,
false,
)) {
println!("发送单播消息给用户: {} 失败: {:?}", to_id, e);
} else {
println!("发送单播消息给用户: {} 成功", to_id);
}
} else {
println!("未找到用户: {} 的sender,无法发送单播消息", to_id);
}
} else {
println!("目标用户: {} 不在线,无法发送单播消息", to_id);
}
}
}
}
}
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