1
0
mirror of https://github.com/actix/examples synced 2024-11-23 22:41:07 +01:00

improve heartbeat with run_interval

This commit is contained in:
bott 2018-08-28 10:59:08 +02:00
parent fb32a7d600
commit b1e5cef4b5
2 changed files with 2 additions and 6 deletions

View File

@ -193,7 +193,7 @@ impl WsChatSession {
/// ///
/// also this method check heartbeats from client /// also this method check heartbeats from client
fn hb(&self, ctx: &mut ws::WebsocketContext<Self, WsChatSessionState>) { fn hb(&self, ctx: &mut ws::WebsocketContext<Self, WsChatSessionState>) {
ctx.run_later(Duration::new(1, 0), |act, ctx| { ctx.run_interval(Duration::new(1, 0), |act, ctx| {
// check client heartbeats // check client heartbeats
if Instant::now().duration_since(act.hb) > Duration::new(10, 0) { if Instant::now().duration_since(act.hb) > Duration::new(10, 0) {
// heartbeat timed out // heartbeat timed out
@ -209,9 +209,6 @@ impl WsChatSession {
} }
ctx.ping(""); ctx.ping("");
// if we can not send message to sink, sink is closed (disconnected)
act.hb(ctx);
}); });
} }
} }

View File

@ -149,7 +149,7 @@ impl ChatSession {
/// ///
/// also this method check heartbeats from client /// also this method check heartbeats from client
fn hb(&self, ctx: &mut Context<Self>) { fn hb(&self, ctx: &mut Context<Self>) {
ctx.run_later(Duration::new(1, 0), |act, ctx| { ctx.run_interval(Duration::new(1, 0), |act, ctx| {
// check client heartbeats // check client heartbeats
if Instant::now().duration_since(act.hb) > Duration::new(10, 0) { if Instant::now().duration_since(act.hb) > Duration::new(10, 0) {
// heartbeat timed out // heartbeat timed out
@ -164,7 +164,6 @@ impl ChatSession {
act.framed.write(ChatResponse::Ping); act.framed.write(ChatResponse::Ping);
// if we can not send message to sink, sink is closed (disconnected) // if we can not send message to sink, sink is closed (disconnected)
act.hb(ctx);
}); });
} }
} }