MCPcopy Create free account
hub / github.com/actix/examples / hb

Method hb

websockets/chat/src/session.rs:37–56  ·  view source on GitHub ↗

helper method that sends ping to client every 5 seconds (HEARTBEAT_INTERVAL). also this method checks heartbeats from client

(&self, ctx: &mut ws::WebsocketContext<Self>)

Source from the content-addressed store, hash-verified

35 ///
36 /// also this method checks heartbeats from client
37 fn hb(&self, ctx: &mut ws::WebsocketContext<Self>) {
38 ctx.run_interval(HEARTBEAT_INTERVAL, |act, ctx| {
39 // check client heartbeats
40 if Instant::now().duration_since(act.hb) > CLIENT_TIMEOUT {
41 // heartbeat timed out
42 println!("Websocket Client heartbeat failed, disconnecting!");
43
44 // notify chat server
45 act.addr.do_send(server::Disconnect { id: act.id });
46
47 // stop actor
48 ctx.stop();
49
50 // don't try to send a ping
51 return;
52 }
53
54 ctx.ping(b"");
55 });
56 }
57}
58
59impl Actor for WsChatSession {

Callers 1

startedMethod · 0.45

Calls 2

stopMethod · 0.80
pingMethod · 0.80

Tested by

no test coverage detected