Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
W
ws-rst
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
qlintonger xeno
ws-rst
Commits
e49f3e28
Commit
e49f3e28
authored
Feb 12, 2025
by
qlintonger xeno
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
成功解决CmdUpdateOnlineUsers的问题
parent
1996b323
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
34 additions
and
46 deletions
+34
-46
src/client.rs
+0
-0
src/events.rs
+13
-11
src/handles/close_connection.rs
+2
-12
src/handles/handshake.rs
+3
-3
src/handles/mod.rs
+3
-4
src/handles/online_users_update.rs
+1
-2
src/handles/redis.rs
+3
-8
src/main.rs
+8
-4
src/typing/message_typed.rs
+1
-2
No files found.
src/client.rs
View file @
e49f3e28
This diff is collapsed.
Click to expand it.
src/events.rs
View file @
e49f3e28
...
@@ -3,7 +3,6 @@ use std::collections::HashMap;
...
@@ -3,7 +3,6 @@ use std::collections::HashMap;
use
std
::
sync
::
Arc
;
use
std
::
sync
::
Arc
;
use
tokio
::
sync
::
mpsc
;
use
tokio
::
sync
::
mpsc
;
use
tokio
::
sync
::
RwLock
;
use
tokio
::
sync
::
RwLock
;
use
tungstenite
::
Error
;
// 定义事件类型
// 定义事件类型
#[derive(Debug)]
#[derive(Debug)]
...
@@ -27,9 +26,14 @@ lazy_static! {
...
@@ -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
;
let
mut
senders
=
CLIENT_SENDERS
.write
()
.await
;
println!
(
"注册客户端的发送者数量---》注册前: {:?}"
,
&
senders
.len
());
senders
.insert
(
from_id
,
center_to_client_sender
);
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>) {
...
@@ -37,13 +41,12 @@ pub async fn handle_events(mut receiver: mpsc::UnboundedReceiver<Event>) {
while
let
Some
(
event
)
=
receiver
.recv
()
.await
{
while
let
Some
(
event
)
=
receiver
.recv
()
.await
{
match
event
{
match
event
{
Event
::
NewConnection
(
from_id
)
=>
{
Event
::
NewConnection
(
from_id
)
=>
{
println!
(
"新连接: {}"
,
from_id
);
println!
(
"有新的连接 用户id {} 更新在线用户列表事件触发"
,
from_id
);
notify_all_clients_to_update_online_users
()
.await
;
}
}
Event
::
CloseConnection
(
from_id
)
=>
{
Event
::
CloseConnection
(
from_id
)
=>
{
println!
(
"关闭连接: {}"
,
from_id
);
println!
(
"有关闭的连接 用户id {} 更新在线用户列表事件触发"
,
from_id
);
// 移除客户端的发送者
notify_all_clients_to_update_online_users
()
.await
;
let
mut
senders
=
CLIENT_SENDERS
.write
()
.await
;
senders
.remove
(
&
from_id
);
}
}
Event
::
UpdateOnlineUsers
=>
{
Event
::
UpdateOnlineUsers
=>
{
// 这里可以实现其他触发更新在线用户列表的逻辑
// 这里可以实现其他触发更新在线用户列表的逻辑
...
@@ -58,9 +61,9 @@ pub async fn handle_events(mut receiver: mpsc::UnboundedReceiver<Event>) {
...
@@ -58,9 +61,9 @@ pub async fn handle_events(mut receiver: mpsc::UnboundedReceiver<Event>) {
// 通知所有客户端线程发送 CmdUpdateOnlineUsers 消息
// 通知所有客户端线程发送 CmdUpdateOnlineUsers 消息
async
fn
notify_all_clients_to_update_online_users
()
{
async
fn
notify_all_clients_to_update_online_users
()
{
let
senders
=
CLIENT_SENDERS
.read
()
.await
;
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
{
if
let
Err
(
e
)
=
sender
.send
(
ClientMessage
::
CmdUpdateOnlineUsers
)
.await
{
println!
(
"通知客户端
更新在线用户列表失败: {:?}"
,
e
);
println!
(
"通知客户端
{} 更新在线用户列表失败: {:?}"
,
from_id
,
e
);
}
}
}
}
}
}
\ No newline at end of file
src/handles/close_connection.rs
View file @
e49f3e28
use
crate
::
events
::
Event
;
use
crate
::
handles
::
redis
::
remove_this_connection
;
use
crate
::
handles
::
redis
::
remove_this_connection
;
use
tokio
::
sync
::
mpsc
::
UnboundedSender
;
pub
async
fn
handle_connection_error
(
pub
async
fn
handle_connection_error
(
from_id
:
&
str
)
{
from_id
:
&
str
,
event_sender
:
&
UnboundedSender
<
Event
>
,
)
{
println!
(
"开始处理用户id: {} 的连接错误"
,
from_id
);
println!
(
"开始处理用户id: {} 的连接错误"
,
from_id
);
// 发送关闭连接事件
event_sender
.send
(
Event
::
CloseConnection
(
from_id
.to_string
()))
.unwrap
();
// 从 Redis 中移除该用户的信息
// 从 Redis 中移除该用户的信息
if
let
Err
(
e
)
=
remove_this_connection
(
from_id
)
.await
{
if
let
Err
(
e
)
=
remove_this_connection
(
from_id
)
.await
{
println!
(
"从 Redis 中移除用户信息时出错: {}"
,
e
);
println!
(
"从 Redis 中移除用户信息时出错: {}"
,
e
);
}
}
}
}
\ No newline at end of file
src/handles/handshake.rs
View file @
e49f3e28
...
@@ -7,7 +7,8 @@ pub(crate) fn handle_handshake(
...
@@ -7,7 +7,8 @@ pub(crate) fn handle_handshake(
static_ws_pwd
:
&
str
,
static_ws_pwd
:
&
str
,
)
->
Result
<
HashMap
<
String
,
String
>
,
String
>
{
)
->
Result
<
HashMap
<
String
,
String
>
,
String
>
{
println!
(
"新客户端连接: {}"
,
req
.uri
());
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
,
Ok
(
p
)
=>
p
,
Err
(
e
)
=>
{
Err
(
e
)
=>
{
let
error_msg
=
format!
(
"缺少重要连接数据段: {}"
,
e
);
let
error_msg
=
format!
(
"缺少重要连接数据段: {}"
,
e
);
...
@@ -32,4 +33,4 @@ pub(crate) fn handle_handshake(
...
@@ -32,4 +33,4 @@ pub(crate) fn handle_handshake(
}
}
Ok
(
connection_params
)
Ok
(
connection_params
)
}
}
\ No newline at end of file
src/handles/mod.rs
View file @
e49f3e28
pub
mod
close_connection
;
pub
mod
handle_messages
;
pub
mod
handle_messages
;
pub
mod
heartbeat
;
pub
mod
handshake
;
pub
mod
handshake
;
pub
mod
heartbeat
;
pub
mod
online_users_update
;
pub
mod
redis
;
pub
mod
redis
;
pub
mod
close_connection
;
pub
mod
online_users_update
;
\ No newline at end of file
src/handles/online_users_update.rs
View file @
e49f3e28
...
@@ -84,4 +84,4 @@ pub async fn send_online_users_resp() -> Result<Vec<(String, String)>, serde_jso
...
@@ -84,4 +84,4 @@ pub async fn send_online_users_resp() -> Result<Vec<(String, String)>, serde_jso
}
}
Ok
(
messages
)
Ok
(
messages
)
}
}
\ No newline at end of file
src/handles/redis.rs
View file @
e49f3e28
use
crate
::
client
::
ONLINE_USERS
;
use
crate
::
config
::
config
::
REDIS_ADDR
;
use
crate
::
config
::
config
::
REDIS_ADDR
;
use
lazy_static
::
lazy_static
;
use
lazy_static
::
lazy_static
;
use
redis
::
Client
;
use
redis
::
Client
;
use
redis
::
Commands
;
use
redis
::
Commands
;
use
redis_pool
::
SingleRedisPool
;
use
redis_pool
::
SingleRedisPool
;
use
std
::
collections
::
HashMap
;
use
std
::
collections
::
HashMap
;
use
crate
::
client
::
ONLINE_USERS
;
lazy_static!
{
lazy_static!
{
static
ref
REDIS_POOL
:
SingleRedisPool
=
{
static
ref
REDIS_POOL
:
SingleRedisPool
=
{
...
@@ -49,11 +49,7 @@ pub async fn insert_this_connection(
...
@@ -49,11 +49,7 @@ pub async fn insert_this_connection(
// callState,channelID,deviceID,fromID,hasCamera,hasMike,isHost,userCallGroup,fromName
// callState,channelID,deviceID,fromID,hasCamera,hasMike,isHost,userCallGroup,fromName
let
user_info_str
=
format!
(
let
user_info_str
=
format!
(
"{},{},{},{},1,1,0,0,{}"
,
"{},{},{},{},1,1,0,0,{}"
,
"idle"
,
"idle"
,
""
,
from_id
,
device_id
,
from_name
""
,
from_id
,
device_id
,
from_name
);
);
if
let
Err
(
e
)
=
con
.hset
::
<&
str
,
&
str
,
&
str
,
()
>
(
"onlineUsers"
,
from_id
,
&
user_info_str
)
{
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(
...
@@ -71,4 +67,4 @@ pub async fn insert_this_connection(
println!
(
"成功将用户id: {} 的信息插入到 ONLINE_USERS 中"
,
from_id
);
println!
(
"成功将用户id: {} 的信息插入到 ONLINE_USERS 中"
,
from_id
);
}
}
Ok
(())
Ok
(())
}
}
\ No newline at end of file
src/main.rs
View file @
e49f3e28
...
@@ -2,10 +2,10 @@ extern crate core;
...
@@ -2,10 +2,10 @@ extern crate core;
mod
client
;
mod
client
;
mod
config
;
mod
config
;
mod
events
;
mod
handles
;
mod
handles
;
mod
typing
;
mod
typing
;
mod
utils
;
mod
utils
;
mod
events
;
use
crate
::
events
::
handle_events
;
use
crate
::
events
::
handle_events
;
use
client
::
handle_client
;
use
client
::
handle_client
;
...
@@ -24,6 +24,11 @@ async fn main() {
...
@@ -24,6 +24,11 @@ async fn main() {
let
client_event_sender
=
event_sender
.clone
();
let
client_event_sender
=
event_sender
.clone
();
// 创建一个用于事件中心向客户端发送消息的通道
// 创建一个用于事件中心向客户端发送消息的通道
let
(
center_to_client_sender
,
center_to_client_receiver
)
=
mpsc
::
channel
(
10
);
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
src/typing/message_typed.rs
View file @
e49f3e28
...
@@ -11,4 +11,4 @@ pub struct ClientMessageData {
...
@@ -11,4 +11,4 @@ pub struct ClientMessageData {
pub
from_name
:
String
,
pub
from_name
:
String
,
#[serde(rename
=
"msgData"
)]
#[serde(rename
=
"msgData"
)]
pub
msg_data
:
serde_json
::
Value
,
pub
msg_data
:
serde_json
::
Value
,
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment