helper method that sends ping to client every second. also this method check heartbeats from client
(&self, ctx: &mut Context<Self>)
| 156 | /// |
| 157 | /// also this method check heartbeats from client |
| 158 | fn hb(&self, ctx: &mut Context<Self>) { |
| 159 | ctx.run_interval(Duration::new(1, 0), |act, ctx| { |
| 160 | // check client heartbeats |
| 161 | if Instant::now().duration_since(act.hb) > Duration::new(10, 0) { |
| 162 | // heartbeat timed out |
| 163 | println!("Client heartbeat failed, disconnecting!"); |
| 164 | |
| 165 | // notify chat server |
| 166 | act.addr.do_send(server::Disconnect { id: act.id }); |
| 167 | |
| 168 | // stop actor |
| 169 | ctx.stop(); |
| 170 | } |
| 171 | |
| 172 | act.framed.write(ChatResponse::Ping); |
| 173 | // if we can not send message to sink, sink is closed (disconnected) |
| 174 | }); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /// Define TCP server that will accept incoming TCP connection and create |