2 chat_server::ChatServer,
3 messages::{Connect, Disconnect, StandardMessage, WsMessage},
8 use actix_web_actors::ws;
9 use lemmy_utils::{utils::get_ip, ConnectionId, IpAddr};
10 use std::time::{Duration, Instant};
11 use tracing::{debug, error, info};
13 /// How often heartbeat pings are sent
14 const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5);
15 /// How long before lack of client response causes a timeout
16 const CLIENT_TIMEOUT: Duration = Duration::from_secs(10);
18 /// Entry point for our route
19 pub async fn chat_route(
22 context: web::Data<LemmyContext>,
23 ) -> Result<HttpResponse, Error> {
26 cs_addr: context.chat_server().to_owned(),
29 ip: get_ip(&req.connection_info()),
37 cs_addr: Addr<ChatServer>,
41 /// Client must send ping at least once per 10 seconds (CLIENT_TIMEOUT),
42 /// otherwise we drop connection.
46 impl Actor for WsSession {
47 type Context = ws::WebsocketContext<Self>;
49 /// Method is called on actor start.
50 /// We register ws session with ChatServer
51 fn started(&mut self, ctx: &mut Self::Context) {
52 // we'll start heartbeat process on session start.
55 // register self in chat server. `AsyncContext::wait` register
56 // future within context, but context waits until this future resolves
57 // before processing any other events.
58 // across all routes within application
59 let addr = ctx.address();
63 addr: addr.recipient(),
64 ip: self.ip.to_owned(),
67 .then(|res, act, ctx| {
69 Ok(res) => act.id = res,
70 // something is wrong with chat server
78 fn stopping(&mut self, _ctx: &mut Self::Context) -> Running {
80 self.cs_addr.do_send(Disconnect {
82 ip: self.ip.to_owned(),
88 /// Handle messages from chat server, we simply send it to peer websocket
89 /// These are room messages, IE sent to others in the room
90 impl Handler<WsMessage> for WsSession {
93 fn handle(&mut self, msg: WsMessage, ctx: &mut Self::Context) {
98 /// WebSocket message handler
99 impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for WsSession {
100 fn handle(&mut self, result: Result<ws::Message, ws::ProtocolError>, ctx: &mut Self::Context) {
101 let message = match result {
109 ws::Message::Ping(msg) => {
110 self.hb = Instant::now();
113 ws::Message::Pong(_) => {
114 self.hb = Instant::now();
116 ws::Message::Text(text) => {
117 let m = text.trim().to_owned();
118 info!("Message received: {:?} from id: {}", &m, self.id);
122 .send(StandardMessage {
127 .then(|res, _, ctx| {
129 Ok(Ok(res)) => ctx.text(res),
131 Err(e) => error!("{}", &e),
133 actix::fut::ready(())
137 ws::Message::Binary(_bin) => info!("Unexpected binary"),
138 ws::Message::Close(_) => {
147 /// helper method that sends ping to client every second.
149 /// also this method checks heartbeats from client
150 fn hb(&self, ctx: &mut ws::WebsocketContext<Self>) {
151 ctx.run_interval(HEARTBEAT_INTERVAL, |act, ctx| {
152 // check client heartbeats
153 if Instant::now().duration_since(act.hb) > CLIENT_TIMEOUT {
154 // heartbeat timed out
155 debug!("Websocket Client heartbeat failed, disconnecting!");
157 // notify chat server
158 act.cs_addr.do_send(Disconnect {
160 ip: act.ip.to_owned(),
166 // don't try to send a ping