Commit e49f3e28 by qlintonger xeno

成功解决CmdUpdateOnlineUsers的问题

parent 1996b323
......@@ -3,7 +3,6 @@ use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::mpsc;
use tokio::sync::RwLock;
use tungstenite::Error;
// 定义事件类型
#[derive(Debug)]
......@@ -27,9 +26,14 @@ lazy_static! {
}
// 注册客户端的发送者
pub async fn register_client(from_id: String, center_to_client_sender: mpsc::Sender<ClientMessage>) {
pub async fn register_client(
from_id: String,
center_to_client_sender: mpsc::Sender<ClientMessage>,
) {
let mut senders = CLIENT_SENDERS.write().await;
println!("注册客户端的发送者数量---》注册前: {:?}", &senders.len());
senders.insert(from_id, center_to_client_sender);
println!("注册客户端的发送者数量---》注册后: {:?}", &senders.len());
}
// 处理事件的任务
......@@ -37,13 +41,12 @@ 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);
println!("有新的连接 用户id {} 更新在线用户列表事件触发", from_id);
notify_all_clients_to_update_online_users().await;
}
Event::CloseConnection(from_id) => {
println!("关闭连接: {}", from_id);
// 移除客户端的发送者
let mut senders = CLIENT_SENDERS.write().await;
senders.remove(&from_id);
println!("有关闭的连接 用户id {} 更新在线用户列表事件触发", from_id);
notify_all_clients_to_update_online_users().await;
}
Event::UpdateOnlineUsers => {
// 这里可以实现其他触发更新在线用户列表的逻辑
......@@ -58,9 +61,9 @@ pub async fn handle_events(mut receiver: mpsc::UnboundedReceiver<Event>) {
// 通知所有客户端线程发送 CmdUpdateOnlineUsers 消息
async fn notify_all_clients_to_update_online_users() {
let senders = CLIENT_SENDERS.read().await;
for (_, sender) in senders.iter() {
for (from_id, sender) in senders.iter() {
if let Err(e) = sender.send(ClientMessage::CmdUpdateOnlineUsers).await {
println!("通知客户端更新在线用户列表失败: {:?}", e);
println!("通知客户端 {} 更新在线用户列表失败: {:?}", from_id, e);
}
}
}
\ No newline at end of file
}
use crate::events::Event;
use crate::handles::redis::remove_this_connection;
use tokio::sync::mpsc::UnboundedSender;
pub async fn handle_connection_error(
from_id: &str,
event_sender: &UnboundedSender<Event>,
) {
pub async fn handle_connection_error(from_id: &str) {
println!("开始处理用户id: {} 的连接错误", from_id);
// 发送关闭连接事件
event_sender.send(Event::CloseConnection(from_id.to_string())).unwrap();
// 从 Redis 中移除该用户的信息
if let Err(e) = remove_this_connection(from_id).await {
println!("从 Redis 中移除用户信息时出错: {}", e);
}
}
\ No newline at end of file
}
......@@ -7,7 +7,8 @@ pub(crate) fn handle_handshake(
static_ws_pwd: &str,
) -> Result<HashMap<String, String>, String> {
println!("新客户端连接: {}", req.uri());
let connection_params = match crate::utils::utils::get_connection_params(req.uri().to_string()) {
let connection_params = match crate::utils::utils::get_connection_params(req.uri().to_string())
{
Ok(p) => p,
Err(e) => {
let error_msg = format!("缺少重要连接数据段: {}", e);
......@@ -32,4 +33,4 @@ pub(crate) fn handle_handshake(
}
Ok(connection_params)
}
\ No newline at end of file
}
pub mod close_connection;
pub mod handle_messages;
pub mod heartbeat;
pub mod handshake;
pub mod heartbeat;
pub mod online_users_update;
pub mod redis;
pub mod close_connection;
pub mod online_users_update;
\ No newline at end of file
......@@ -84,4 +84,4 @@ pub async fn send_online_users_resp() -> Result<Vec<(String, String)>, serde_jso
}
Ok(messages)
}
\ No newline at end of file
}
use crate::client::ONLINE_USERS;
use crate::config::config::REDIS_ADDR;
use lazy_static::lazy_static;
use redis::Client;
use redis::Commands;
use redis_pool::SingleRedisPool;
use std::collections::HashMap;
use crate::client::ONLINE_USERS;
lazy_static! {
static ref REDIS_POOL: SingleRedisPool = {
......@@ -49,11 +49,7 @@ pub async fn insert_this_connection(
// callState,channelID,deviceID,fromID,hasCamera,hasMike,isHost,userCallGroup,fromName
let user_info_str = format!(
"{},{},{},{},1,1,0,0,{}",
"idle",
"",
from_id,
device_id,
from_name
"idle", "", from_id, device_id, from_name
);
if let Err(e) = con.hset::<&str, &str, &str, ()>("onlineUsers", from_id, &user_info_str) {
......@@ -71,4 +67,4 @@ pub async fn insert_this_connection(
println!("成功将用户id: {} 的信息插入到 ONLINE_USERS 中", from_id);
}
Ok(())
}
\ No newline at end of file
}
......@@ -2,10 +2,10 @@ extern crate core;
mod client;
mod config;
mod events;
mod handles;
mod typing;
mod utils;
mod events;
use crate::events::handle_events;
use client::handle_client;
......@@ -24,6 +24,11 @@ async fn main() {
let client_event_sender = event_sender.clone();
// 创建一个用于事件中心向客户端发送消息的通道
let (center_to_client_sender, center_to_client_receiver) = mpsc::channel(10);
tokio::spawn(handle_client(stream, client_event_sender, center_to_client_sender, center_to_client_receiver));
tokio::spawn(handle_client(
stream,
client_event_sender,
center_to_client_sender,
center_to_client_receiver,
));
}
}
\ No newline at end of file
}
......@@ -11,4 +11,4 @@ pub struct ClientMessageData {
pub from_name: String,
#[serde(rename = "msgData")]
pub msg_data: serde_json::Value,
}
\ 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