use actix::Actor;
use actix_web_actors::ws;
use actix::prelude::*;
use crate::websocket::webchat::actions::Connect;
use crate::websocket::webchat::actions::Disconnect;
use crate::websocket::webchat::connection::Connection;
impl Actor for Connection {
type Context = ws::WebsocketContext<Self>;
fn started(&mut self, ctx: &mut Self::Context) {
self.ping(ctx);
let addr = ctx.address();
self.addr
.send(
Connect {
addr: addr.recipient(),
id: self.id.to_owned(),
user_details: self.requestor.get_user().to_owned().cloned(),
},
)
.into_actor(self)
.then(|res, _, ctx| {
match res {
Ok(_res) => (),
_ => ctx.stop(),
}
fut::ready(())
})
.wait(ctx);
}
fn stopping(&mut self, _ctx: &mut Self::Context) -> Running {
self.addr.do_send(
Disconnect {
id: self.id.to_owned(),
},
);
Running::Stop
}
}