wewo
This commit is contained in:
parent
3cb2f0ec5e
commit
7baa5a52fe
4 changed files with 27 additions and 25 deletions
|
|
@ -1,15 +1,15 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use futures::lock::Mutex;
|
||||
use tokio::sync::mpsc::UnboundedSender;
|
||||
use tokio::sync::{RwLock, mpsc::UnboundedSender};
|
||||
use tungstenite::Utf8Bytes;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub struct Caller {
|
||||
pub user_id: Mutex<Uuid>,
|
||||
pub user_id: RwLock<Uuid>,
|
||||
pub tx: Mutex<Option<UnboundedSender<Utf8Bytes>>>,
|
||||
pub user_state: Mutex<CallUserState>,
|
||||
pub streaming: Mutex<bool>,
|
||||
pub user_state: RwLock<CallUserState>,
|
||||
pub streaming: RwLock<bool>,
|
||||
}
|
||||
#[derive(Clone)]
|
||||
pub enum CallUserState {
|
||||
|
|
@ -21,26 +21,26 @@ pub enum CallUserState {
|
|||
impl Caller {
|
||||
pub fn new(user_id: Uuid, tx: UnboundedSender<Utf8Bytes>) -> Self {
|
||||
Self {
|
||||
user_id: Mutex::new(user_id),
|
||||
user_id: RwLock::new(user_id),
|
||||
tx: Mutex::new(Some(tx)),
|
||||
user_state: Mutex::new(CallUserState::Active),
|
||||
streaming: Mutex::new(false),
|
||||
user_state: RwLock::new(CallUserState::Active),
|
||||
streaming: RwLock::new(false),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn send(&self, msg: impl Into<Utf8Bytes>) {
|
||||
if let Some(tx) = &self.tx.lock().await {
|
||||
pub async fn send(&self, msg: impl Into<Utf8Bytes>) {
|
||||
if let Some(tx) = &*self.tx.lock().await {
|
||||
let _ = tx.send(msg.into());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn disconnect(self: Arc<Self>) {
|
||||
self.user_state = CallUserState::Disconnected;
|
||||
self.tx = None;
|
||||
pub async fn disconnect(self: &Arc<Self>) {
|
||||
*self.user_state.write().await = CallUserState::Disconnected;
|
||||
*self.tx.lock().await = None;
|
||||
}
|
||||
|
||||
pub fn is_connected(&self) -> bool {
|
||||
if let CallUserState::Disconnected = self.user_state {
|
||||
pub async fn is_connected(&self) -> bool {
|
||||
if let CallUserState::Disconnected = *self.user_state.read().await {
|
||||
false
|
||||
} else {
|
||||
true
|
||||
|
|
|
|||
Loading…
Reference in a new issue