use tokio::sync::{mpsc, oneshot};
use super::{Command, ConnId};
#[derive(Clone)]
pub struct ChatServerHandle {
pub cmd_tx: mpsc::UnboundedSender<Command>,
pub context: actix_web::web::Data<crate::server::AppContext>,
}
impl ChatServerHandle {
pub async fn connect(&self, conn_tx: mpsc::UnboundedSender<String>) -> ConnId {
let (res_tx, res_rx) = oneshot::channel();
self.cmd_tx
.send(Command::Connect { conn_tx, res_tx })
.unwrap();
res_rx.await.unwrap()
}
pub async fn list_rooms(&self) -> Vec<String> {
let (res_tx, res_rx) = oneshot::channel();
self.cmd_tx.send(Command::List { res_tx }).unwrap();
res_rx.await.unwrap()
}
pub async fn join_room(&self, conn: ConnId, room: impl Into<String>) {
let (res_tx, res_rx) = oneshot::channel();
self.cmd_tx
.send(Command::Join {
conn,
room: room.into(),
res_tx,
})
.unwrap();
res_rx.await.unwrap();
}
pub async fn send_message(&self, conn: ConnId, msg: impl Into<String>) {
let (res_tx, res_rx) = oneshot::channel();
self.cmd_tx
.send(Command::Message {
msg: msg.into(),
conn,
res_tx,
})
.unwrap();
res_rx.await.unwrap();
}
pub fn disconnect(&self, conn: ConnId) {
self.cmd_tx.send(Command::Disconnect { conn }).unwrap();
}
}