zero4rs 2.0.0

zero4rs is a powerful, pragmatic, and extremely fast web framework for Rust
Documentation
use serde_json::json;

use actix::Context;
use actix::Handler;

use crate::websocket::webchat::actions::Broadcast;
use crate::websocket::webchat::actions::Connect;
use crate::websocket::webchat::actions::ConnectionWarnning;
use crate::websocket::webchat::actions::Disconnect;
use crate::websocket::webchat::actions::ServerTime;
use crate::websocket::webchat::input::subscribes::SubscribeAction;
use crate::websocket::webchat::input::subscribes::SubscribeChannel;
use crate::websocket::webchat::input::subscribes::UnsubscribeAction;
use crate::websocket::webchat::output::Level;
use crate::websocket::webchat::output::OutputMessageType;
use crate::websocket::webchat::server::Server;

// 广播一条消息
impl Handler<Broadcast> for Server {
    type Result = ();

    fn handle(&mut self, msg: Broadcast, _: &mut Context<Self>) {
        self.connections.iter().for_each(|(id, _)| {
            self.send_message(msg.level, msg.ty, id, &msg.msg, None);
        });
    }
}

// 发送一条消息,已成功建立连接
impl Handler<Connect> for Server {
    type Result = ();

    fn handle(&mut self, connect: Connect, _: &mut Context<Self>) -> Self::Result {
        let connect_id = connect.id;

        self.connections.insert(connect_id.to_owned(), connect.addr);

        if let Some(user_details) = connect.user_details {
            self.user_infos.insert(connect_id.to_owned(), user_details);
        }

        self.send_json(
            Level::Info,
            OutputMessageType::Connected,
            &connect_id,
            json! ({"connect_id": &connect_id}),
            None,
        );
    }
}

// 发送一条消息,连接已断开
impl Handler<Disconnect> for Server {
    type Result = ();

    fn handle(&mut self, msg: Disconnect, _: &mut Context<Self>) {
        if self.connections.contains_key(&msg.id) {
            self.send_message(
                Level::Warn,
                OutputMessageType::Disconnect,
                &msg.id,
                "Bye bye.",
                None,
            );

            self.destroy(&msg.id);
        }
    }
}

// 发送一条消息,告知即将断开连接
impl Handler<ConnectionWarnning> for Server {
    type Result = ();

    fn handle(&mut self, msg: ConnectionWarnning, _: &mut Context<Self>) {
        if self.connections.contains_key(&msg.id) {
            self.send_message(
                Level::Warn,
                OutputMessageType::Disconnect,
                &msg.id,
                &format!("Your conneciton will be closed at {}", msg.at),
                None,
            );
        }
    }
}

// 发送一条消息,告知当前服务器事件
impl Handler<ServerTime> for Server {
    type Result = ();

    fn handle(&mut self, msg: ServerTime, _: &mut Context<Self>) {
        self.subscribes.iter().for_each(|(id, channels)| {
            if channels.contains(&SubscribeChannel::ServerTime.to_string()) {
                self.send_message(
                    Level::Info,
                    OutputMessageType::ServerTime,
                    id,
                    &msg.time,
                    None,
                );
            }
        });
    }
}

impl Handler<SubscribeAction> for Server {
    type Result = ();

    fn handle(&mut self, msg: SubscribeAction, _: &mut Context<Self>) {
        let subscribes = &mut self.subscribes;
        let channel = msg.channel.to_string();

        if let Some(channels) = subscribes.get_mut(&msg.id) {
            if !channels.contains(&channel) {
                channels.push(channel);
            }
        } else {
            subscribes.insert(msg.id.to_owned(), vec![channel]);
        }
    }
}

impl Handler<UnsubscribeAction> for Server {
    type Result = ();

    fn handle(&mut self, msg: UnsubscribeAction, _: &mut Context<Self>) {
        let subscribes = &mut self.subscribes;
        let channel = msg.channel.to_string();

        if let Some(channels) = subscribes.get_mut(&msg.id) {
            if channels.contains(&channel) {
                channels.retain(|x| *x != channel);
            }
        }
    }
}