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>)
| 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 | |
| 59 | impl Actor for WsChatSession { |