Commit 2579a50d by qlintonger xeno

尝试对接声网有关操作以及音视频+1

parent 1bc4703d
use crate::config::config::STATIC_WS_PWD; use crate::config::config::STATIC_WS_PWD;
use crate::deport::handle_ws_msg; use crate::deport::handle_ws_msg;
use crate::events::{register_client, ClientMessage, Event}; use crate::events::{register_client, ClientMessage, Event};
use crate::handles::handshake::handle_handshake; use crate::handles::handshake::handle_websocket_handshake;
use crate::handles::heartbeat::heart_resp; use crate::handles::heartbeat::heart_resp;
use crate::handles::online_users_update::send_online_users_and_send; use crate::handles::online_users_update::send_online_users_and_send;
use crate::utils::json_utils::parse_message; use crate::utils::json_utils::parse_message;
...@@ -12,8 +12,6 @@ use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH}; ...@@ -12,8 +12,6 @@ use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
use tokio::sync::mpsc::{Receiver, Sender, UnboundedSender}; use tokio::sync::mpsc::{Receiver, Sender, UnboundedSender};
use tokio::sync::watch; use tokio::sync::watch;
use tokio::time; use tokio::time;
use tokio_tungstenite::accept_hdr_async;
use tungstenite::handshake::server::{Request, Response};
use tungstenite::{Error, Message}; use tungstenite::{Error, Message};
lazy_static! { lazy_static! {
...@@ -29,31 +27,14 @@ pub(crate) async fn handle_client( ...@@ -29,31 +27,14 @@ pub(crate) async fn handle_client(
let must_existed_params = ["deviceID", "fromID", "wsPwd"]; let must_existed_params = ["deviceID", "fromID", "wsPwd"];
let mut connection_params = None; let mut connection_params = None;
let ws_stream = match accept_hdr_async(stream, |req: &Request, resp| { let ws_stream =
match handle_handshake(req, &must_existed_params, STATIC_WS_PWD) { match handle_websocket_handshake(stream, &must_existed_params, STATIC_WS_PWD, &mut connection_params).await {
Ok(params) => { Ok(ws) => ws,
connection_params = Some(params); Err(e) => {
Ok(resp) println!("WebSocket握手失败: {:?}", e);
return Ok(());
} }
Err(error_msg) => { };
println!("{}", error_msg);
let error_resp = Response::builder()
.status(400)
.header("Content-Type", "text/plain")
.body(Some(error_msg))
.unwrap();
Err(error_resp)
}
}
})
.await
{
Ok(ws) => ws,
Err(e) => {
println!("WebSocket握手失败: {}", e);
return Ok(());
}
};
let (mut sender, mut receiver) = ws_stream.split(); let (mut sender, mut receiver) = ws_stream.split();
let mut last_heartbeat_time = Instant::now(); // 包装为 Arc<Mutex<Instant>> let mut last_heartbeat_time = Instant::now(); // 包装为 Arc<Mutex<Instant>>
...@@ -99,11 +80,7 @@ pub(crate) async fn handle_client( ...@@ -99,11 +80,7 @@ pub(crate) async fn handle_client(
break; break;
} }
} }
ClientMessage::SendClientMessage( ClientMessage::SendClientMessage(from_id, client_message, close) => {
from_id,
client_message,
close
) => {
let real_user_id = from_id.0; let real_user_id = from_id.0;
if let Err(e) = sender.send(Message::text(client_message)).await { if let Err(e) = sender.send(Message::text(client_message)).await {
println!("发送给用户id {} 独立消息失败:{:?}", real_user_id, e); println!("发送给用户id {} 独立消息失败:{:?}", real_user_id, e);
...@@ -113,7 +90,7 @@ pub(crate) async fn handle_client( ...@@ -113,7 +90,7 @@ pub(crate) async fn handle_client(
// 通知外层循环关闭 // 通知外层循环关闭
close_tx.send(true).unwrap(); close_tx.send(true).unwrap();
println!("发送给用户id {} 要求关闭连接", real_user_id); println!("发送给用户id {} 要求关闭连接", real_user_id);
break break;
} }
} }
} }
...@@ -144,7 +121,7 @@ pub(crate) async fn handle_client( ...@@ -144,7 +121,7 @@ pub(crate) async fn handle_client(
let event_sender_clone = event_sender.clone(); let event_sender_clone = event_sender.clone();
let connection_time_clone = connection_time.clone(); let connection_time_clone = connection_time.clone();
tokio::spawn(async move { tokio::spawn(async move {
handle_ws_msg(data.msg_type, from_id_clone, event_sender_clone, connection_time_clone).await; handle_ws_msg(&data, from_id_clone, event_sender_clone, connection_time_clone).await;
}); });
} }
} }
......
use crate::events::Event; use crate::events::Event;
use crate::handles::handle_agora_call::handle_agora_call;
use crate::handles::online_users_update::{online_messages, send_online_users_resp}; use crate::handles::online_users_update::{online_messages, send_online_users_resp};
use crate::typing::message_typed::ClientMessageData;
use tokio::sync::mpsc::UnboundedSender; use tokio::sync::mpsc::UnboundedSender;
use crate::handles::handle_agora_call::handle_agora_call;
pub async fn handle_ws_msg( pub async fn handle_ws_msg(
msg_type: String, client_message_data: &ClientMessageData,
from_id: String, from_id: String,
event_sender: UnboundedSender<Event>, event_sender: UnboundedSender<Event>,
connection_time: u128, connection_time: u128,
) { ) {
let msg_type = client_message_data.msg_type.clone();
match msg_type.as_str() { match msg_type.as_str() {
"GetCompanyUserList" => { "GetCompanyUserList" => {
println!("收到客户端获取在线用户列表 {}", &from_id); println!("收到客户端获取在线用户列表 {}", &from_id);
...@@ -30,9 +32,18 @@ pub async fn handle_ws_msg( ...@@ -30,9 +32,18 @@ pub async fn handle_ws_msg(
} }
} }
} }
"Call" | "CancelCall" => { // 通话类消息直接托管给对应句柄即可
handle_agora_call(&msg_type, &from_id, &event_sender, &connection_time).await; "Call" | "CancelCall" | "Refuse" | "EndMeeting" | "Hangup" | "Connect" | "Mute"
| "MuteAll" | "KickOut" | "MuteSelf" | "UnMuteSelf" => {
handle_agora_call(
&client_message_data,
&from_id,
&event_sender,
&connection_time,
)
.await;
} }
// 针对其余消息类型,直接根据fromID -> toID规则发送即可
_ => {} _ => {}
} }
} }
...@@ -125,7 +125,7 @@ pub async fn handle_events(mut receiver: mpsc::UnboundedReceiver<Event>) { ...@@ -125,7 +125,7 @@ pub async fn handle_events(mut receiver: mpsc::UnboundedReceiver<Event>) {
} }
// 通知所有客户端线程发送 CmdUpdateOnlineUsers 消息 // 通知所有客户端线程发送 CmdUpdateOnlineUsers 消息
async fn notify_all_clients_to_update_online_users() { pub async fn notify_all_clients_to_update_online_users() {
println!( println!(
"尝试发送在新用户更新消息体,目前总人数为:{}", "尝试发送在新用户更新消息体,目前总人数为:{}",
CLIENT_SENDERS.len() CLIENT_SENDERS.len()
......
use std::collections::HashMap; use std::collections::HashMap;
use tokio::net::TcpStream;
use tokio_tungstenite::{accept_hdr_async, WebSocketStream};
use tungstenite::handshake::client::Request;
use tungstenite::handshake::server::Response;
// 提取出来的处理握手的函数 // 提取出来的处理握手的函数
pub(crate) fn handle_handshake( pub(crate) fn handle_handshake(
...@@ -38,3 +42,39 @@ pub(crate) fn handle_handshake( ...@@ -38,3 +42,39 @@ pub(crate) fn handle_handshake(
Ok(connection_params) Ok(connection_params)
} }
pub async fn handle_websocket_handshake(
stream: TcpStream,
must_existed_params: &[&str],
static_ws_pwd: &str,
connection_params: &mut Option<HashMap<String, String>>
) -> Result<WebSocketStream<TcpStream>, ()> {
let ws_stream = match accept_hdr_async(stream, |req: &Request, resp| {
match handle_handshake(req, must_existed_params, static_ws_pwd) {
Ok(params) => {
*connection_params = Some(params);
Ok(resp)
}
Err(error_msg) => {
println!("{}", error_msg);
let error_resp = Response::builder()
.status(400)
.header("Content-Type", "text/plain")
.body(Some(error_msg))
.unwrap();
Err(error_resp)
}
}
})
.await
{
Ok(ws) => ws,
Err(e) => {
println!("WebSocket握手失败: {}", e);
return Err(());
}
};
Ok(ws_stream)
}
...@@ -31,6 +31,18 @@ pub async fn remove_this_connection(from_id: &str) -> Result<(), redis::RedisErr ...@@ -31,6 +31,18 @@ pub async fn remove_this_connection(from_id: &str) -> Result<(), redis::RedisErr
Ok(()) Ok(())
} }
pub async fn update_client_redis_data(from_id: &str, data_str: String) -> Result<(), redis::RedisError> {
// 修改对应用户redis数据
let mut con = REDIS_POOL
.get_connection()
.expect("Failed to get Redis connection");
if let Err(e) = con.hset::<&str, &str, &str, ()>("onlineUsers", from_id, &data_str) {
println!("修改 Redis 中的 onlineUsers 哈希表时出错: {}", e);
return Err(e);
}
Ok(())
}
// 将当前用户的信息插入到 Redis 的 onlineUsers 集合中 // 将当前用户的信息插入到 Redis 的 onlineUsers 集合中
pub async fn insert_this_connection( pub async fn insert_this_connection(
from_id: &str, from_id: &str,
......
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