zero4rs 2.0.0

zero4rs is a powerful, pragmatic, and extremely fast web framework for Rust
Documentation
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 (asynchronously), or do_send (同步)
            .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(),
            },
        );

        // ctx.close(None);

        Running::Stop
    }
}