zero4rs 2.0.0

zero4rs is a powerful, pragmatic, and extremely fast web framework for Rust
Documentation
use tokio::sync::{mpsc, oneshot};

use super::{
    command::Command,
    connection_new::Connection,
    output::{Level, Ty},
    ConnId, UserInfo,
};

impl Connection {
    /// Register client message sender and obtain connection ID.
    pub async fn connect(
        &self,
        conn_tx: mpsc::UnboundedSender<String>,
        user_info: UserInfo,
    ) -> ConnId {
        let (res_tx, res_rx) = oneshot::channel();

        // unwrap: chat server should not have been dropped
        self.cmd_tx
            .send(Command::Connect {
                conn_tx,
                res_tx,
                user_info,
            })
            .unwrap_or_else(|e| {
                log::error!("error={:?}", e);
            });

        // unwrap: chat server does not drop out response channel
        res_rx.await.unwrap()
    }

    /// Unregister message sender and broadcast disconnection message to current room.
    pub fn disconnect(&self, conn: ConnId) {
        // unwrap: server should not have been dropped
        self.cmd_tx
            .send(Command::Disconnect { conn })
            .unwrap_or_else(|e| {
                log::error!("error={:?}", e);
            });
    }

    /// get total connections count
    pub async fn total(&self) -> usize {
        let (res_tx, res_rx) = oneshot::channel();

        self.cmd_tx
            .send(Command::Total { res_tx })
            .unwrap_or_else(|e| {
                log::error!("error={:?}", e);
            });

        res_rx.await.unwrap_or(0)
    }

    /// 返回所有在线用户
    pub async fn users_online(&self) -> Vec<String> {
        let (res_tx, res_rx) = oneshot::channel();

        self.cmd_tx
            .send(Command::OnlineUsers { res_tx })
            .unwrap_or_else(|e| {
                log::error!("error={:?}", e);
            });

        res_rx.await.unwrap_or(Vec::new())
    }

    /// 返回所有连接
    pub async fn all_connections(&self) -> Vec<(String, String, String)> {
        let (res_tx, res_rx) = oneshot::channel();

        self.cmd_tx
            .send(Command::AllConnections { res_tx })
            .unwrap_or_else(|e| {
                log::error!("error={:?}", e);
            });

        res_rx.await.unwrap_or(Vec::new())
    }

    /// closing a connction
    pub fn closing(&self, conn: ConnId) {
        self.cmd_tx
            .send(Command::Closed { conn })
            .unwrap_or_else(|e| {
                log::error!("error={:?}", e);
            });
    }

    /// 广播一条消息
    pub fn broadcast(&self, level: Level, ty: Ty, msg: String) {
        self.cmd_tx
            .send(Command::Broadcast { level, ty, msg })
            .unwrap_or_else(|e| {
                log::error!("error={:?}", e);
            });
    }
}