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
1996b323
Commit
1996b323
authored
Feb 12, 2025
by
qlintonger xeno
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
尝试使用事件机制来实现多线程沟通+1.55
parent
1b49c3a6
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
14 additions
and
40 deletions
+14
-40
src/client.rs
+14
-11
src/events.rs
+0
-18
src/typing/mod.rs
+0
-1
src/typing/used_typed.rs
+0
-10
No files found.
src/client.rs
View file @
1996b323
use
crate
::
config
::
config
::
STATIC_WS_PWD
;
use
crate
::
events
::{
Event
,
register_client
,
ClientMessage
};
use
crate
::
events
::{
register_client
,
ClientMessage
,
Event
};
use
crate
::
handles
::
close_connection
::
handle_connection_error
;
use
crate
::
handles
::
handle_messages
::
handle_other_message
;
use
crate
::
handles
::
handshake
::
handle_handshake
;
...
...
@@ -12,8 +12,8 @@ use lazy_static::lazy_static;
use
std
::
collections
::
HashMap
;
use
std
::
sync
::
Arc
;
use
std
::
time
::{
Duration
,
Instant
};
use
tokio
::
sync
::
mpsc
::{
UnboundedSender
,
Sender
,
Receiv
er
};
use
tokio
::
sync
::
{
mpsc
,
RwLock
as
AsyncRwLock
}
;
use
tokio
::
sync
::
mpsc
::{
Receiver
,
Sender
,
UnboundedSend
er
};
use
tokio
::
sync
::
RwLock
as
AsyncRwLock
;
use
tokio
::
time
;
use
tokio_tungstenite
::
accept_hdr_async
;
use
tungstenite
::
handshake
::
server
::{
Request
,
Response
};
...
...
@@ -157,19 +157,12 @@ pub(crate) async fn handle_client(
}
}
}
// 处理心跳超时
_
=
time
::
sleep_until
(
tokio
::
time
::
Instant
::
from
(
last_heartbeat_time
+
Duration
::
from_secs
(
20
)))
=>
{
println!
(
"用户id-{} 20秒内没有发送心跳,挂断连接"
,
from_id_clone
);
// 发送关闭连接事件
event_sender
.send
(
Event
::
CloseConnection
(
from_id_clone
.clone
()))
.unwrap
();
handle_connection_error
(
&
from_id_clone
,
&
event_sender
)
.await
;
break
;
}
// 处理来自事件中心的消息
maybe_msg
=
center_to_client_receiver
.recv
()
=>
{
if
let
Some
(
msg
)
=
maybe_msg
{
match
msg
{
ClientMessage
::
CmdUpdateOnlineUsers
=>
{
println!
(
"消息中心:==> 收到 CmdUpdateOnlineUsers 消息"
);
if
let
Err
(
e
)
=
send_online_users_and_send
(
&
mut
sender
,
&
from_id_clone
)
.await
{
println!
(
"处理在线用户列表出错了:{:?}"
,
e
);
// 发送关闭连接事件
...
...
@@ -182,6 +175,14 @@ pub(crate) async fn handle_client(
}
}
}
// 处理心跳超时
_
=
time
::
sleep_until
(
tokio
::
time
::
Instant
::
from
(
last_heartbeat_time
+
Duration
::
from_secs
(
20
)))
=>
{
println!
(
"用户id-{} 20秒内没有发送心跳,挂断连接"
,
from_id_clone
);
// 发送关闭连接事件
event_sender
.send
(
Event
::
CloseConnection
(
from_id_clone
.clone
()))
.unwrap
();
handle_connection_error
(
&
from_id_clone
,
&
event_sender
)
.await
;
break
;
}
}
}
println!
(
"断开与用户id: {},连接"
,
from_id_clone
);
...
...
@@ -207,5 +208,6 @@ async fn send_online_users_and_send(sender: &mut (impl SinkExt<Message, Error =
return
Err
(
e
);
}
}
println!
(
"发送在线用户列表消息给用户 {} 成功"
,
from_id
);
Ok
(())
}
\ No newline at end of file
src/events.rs
View file @
1996b323
use
futures
::
SinkExt
;
use
lazy_static
::
lazy_static
;
use
std
::
collections
::
HashMap
;
use
std
::
sync
::
Arc
;
...
...
@@ -6,12 +5,6 @@ use tokio::sync::mpsc;
use
tokio
::
sync
::
RwLock
;
use
tungstenite
::
Error
;
// 假设的用户详细信息结构体
#[derive(Debug)]
pub
struct
UserDetails
{
pub
username
:
String
,
}
// 定义事件类型
#[derive(Debug)]
pub
enum
Event
{
...
...
@@ -62,17 +55,6 @@ pub async fn handle_events(mut receiver: mpsc::UnboundedReceiver<Event>) {
}
}
// 向指定客户端发送消息
async
fn
send_message_to_client
(
from_id
:
&
str
,
message
:
String
)
->
Result
<
(),
Error
>
{
let
senders
=
CLIENT_SENDERS
.read
()
.await
;
if
let
Some
(
sender
)
=
senders
.get
(
from_id
)
{
if
let
Err
(
e
)
=
sender
.send
(
ClientMessage
::
SendMessage
(
message
))
.await
{
return
Err
(
Error
::
Io
(
std
::
io
::
Error
::
new
(
std
::
io
::
ErrorKind
::
Other
,
e
)));
}
}
Ok
(())
}
// 通知所有客户端线程发送 CmdUpdateOnlineUsers 消息
async
fn
notify_all_clients_to_update_online_users
()
{
let
senders
=
CLIENT_SENDERS
.read
()
.await
;
...
...
src/typing/mod.rs
View file @
1996b323
pub
mod
used_typed
;
pub
mod
message_typed
;
src/typing/used_typed.rs
deleted
100644 → 0
View file @
1b49c3a6
use
tokio_tungstenite
::
WebSocketStream
;
use
tungstenite
::
Message
;
// 自定义结构体来存储发送器和接收器
#[derive(Debug)]
pub
struct
Connection
{
pub
(
crate
)
sender
:
futures
::
stream
::
SplitSink
<
WebSocketStream
<
tokio
::
net
::
TcpStream
>
,
Message
>
,
pub
(
crate
)
receiver
:
futures
::
stream
::
SplitStream
<
WebSocketStream
<
tokio
::
net
::
TcpStream
>>
,
}
\ 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