Commit e1abb6bd by qlintonger xeno

对接GetCompanyUserList成功

parent d6ca5e6c
...@@ -140,7 +140,12 @@ pub(crate) async fn handle_client( ...@@ -140,7 +140,12 @@ pub(crate) async fn handle_client(
} }
}, },
_ => { _ => {
handle_ws_msg(data.msg_type, from_id.clone(), event_sender.clone()); let from_id_clone = from_id.clone();
let event_sender_clone = event_sender.clone();
let connection_time_clone = connection_time.clone();
tokio::spawn(async move {
handle_ws_msg(data.msg_type, from_id_clone, event_sender_clone, connection_time_clone).await;
});
} }
} }
} }
......
use crate::events::Event; use crate::events::Event;
use crate::handles::online_users_update::{online_messages, send_online_users_resp};
use tokio::sync::mpsc::UnboundedSender; use tokio::sync::mpsc::UnboundedSender;
pub fn handle_ws_msg( pub async fn handle_ws_msg(
msg_type: String, msg_type: String,
from_id: String, from_id: String,
event_sender: UnboundedSender<Event>, event_sender: UnboundedSender<Event>,
connection_time: u128,
) { ) {
match msg_type.as_str() { match msg_type.as_str() {
"GetCompanyUserList" => { "GetCompanyUserList" => {
println!("收到客户端获取在线用户列表 {}", &from_id); println!("收到客户端获取在线用户列表 {}", &from_id);
}, // 需要主动发送在线人员更新事件
_ => { if let Ok(online_users_json) = send_online_users_resp().await {
if let Ok(formatted_json) = online_messages(online_users_json, from_id.clone()) {
if let Err(e) = event_sender.send(Event::SendClientMessage(
(from_id.clone(), connection_time),
formatted_json,
false,
)) {
println!(
"客户端要求更新在线列表 {}发送在线用户列表失败 {:?}",
&from_id, e
);
} else {
println!("客户端要求更新在线列表 {}发送在线用户列表成功", &from_id);
}
}
}
} }
_ => {}
} }
} }
...@@ -42,6 +42,17 @@ pub struct ServerOnlineUserMessage { ...@@ -42,6 +42,17 @@ pub struct ServerOnlineUserMessage {
pub time: u128, pub time: u128,
} }
pub fn online_messages(messages: Vec<OnlineUserMessage>, from_id: String) -> serde_json::Result<String> {
serde_json::to_string(&ServerOnlineUserMessage {
msg_type: "CmdUpdateUserList".to_string(),
from_name: "Server".to_string(),
from_id: "0".to_string(),
to_id: from_id,
msg_data: messages,
time: get_current_timestamp(),
})
}
pub async fn send_online_users_and_send( pub async fn send_online_users_and_send(
sender: &mut (impl SinkExt<Message, Error=Error> + Unpin), sender: &mut (impl SinkExt<Message, Error=Error> + Unpin),
from_id: &str, from_id: &str,
...@@ -49,14 +60,7 @@ pub async fn send_online_users_and_send( ...@@ -49,14 +60,7 @@ pub async fn send_online_users_and_send(
let messages = send_online_users_resp() let messages = send_online_users_resp()
.await .await
.map_err(|e| Error::Io(std::io::Error::new(std::io::ErrorKind::Other, e)))?; .map_err(|e| Error::Io(std::io::Error::new(std::io::ErrorKind::Other, e)))?;
let json_message = serde_json::to_string(&ServerOnlineUserMessage { let json_message = online_messages(messages, from_id.to_string())
msg_type: "CmdUpdateUserList".to_string(),
from_name: "Server".to_string(),
from_id: "0".to_string(),
to_id: from_id.to_string(),
msg_data: messages,
time: get_current_timestamp(),
})
.map_err(|e| Error::Io(std::io::Error::new(std::io::ErrorKind::Other, e)))?; .map_err(|e| Error::Io(std::io::Error::new(std::io::ErrorKind::Other, e)))?;
if let Err(e) = sender.send(Message::text(json_message)).await { if let Err(e) = sender.send(Message::text(json_message)).await {
println!("发送在线用户列表消息给用户 {} 失败: {}", from_id, e); println!("发送在线用户列表消息给用户 {} 失败: {}", from_id, e);
......
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