diff --git a/Cargo.toml b/Cargo.toml index 2e9fe3a..1b60d04 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ferrisbox" -version = "0.1.0" +version = "0.2.0" edition = "2021" [dependencies] @@ -9,3 +9,4 @@ serde = { version = "1.0.210", features = ["derive"] } serde_json = "1.0.128" tokio = { version = "1.40.0", features = ["rt-multi-thread", "sync", "macros"] } tokio-tungstenite = { version = "0.24.0", features = ["native-tls"] } +tracing = "0.1.44" diff --git a/src/lib.rs b/src/lib.rs index 78c3711..62cceac 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,15 +8,19 @@ use tokio_tungstenite::{connect_async, tungstenite::Message}; pub mod models; pub mod packets; +#[derive(Debug)] +pub struct ChatboxEventLoop { + rx: Receiver, +} + #[derive(Debug)] pub struct ChatboxClientInstance { tx: Sender, - rx: Receiver, last_message_id: AtomicI32, } impl ChatboxClientInstance { - pub async fn new(license: String, endpoint: Option<&str>) -> Self { + pub async fn new(license: String, endpoint: Option<&str>) -> (ChatboxClientInstance, ChatboxEventLoop) { let endpoint = endpoint.unwrap_or("wss://chat.reconnected.cc/v2"); let url = format!("{}/{}", endpoint, license); @@ -34,10 +38,10 @@ impl ChatboxClientInstance { let message = message.into_text().expect("Got invalid UTF8"); if let Ok(packet) = serde_json::from_str::(&message) { - ws_tx - .send(packet) - .await - .expect("Failed to send packet to reciever.") + if let Err(e) = ws_tx.send(packet).await { + tracing::error!("Failed to send packet to reciever: {}", e); + break; + } } } } @@ -56,14 +60,15 @@ impl ChatboxClientInstance { } }); - ChatboxClientInstance { + (ChatboxClientInstance { tx, - rx: ws_rx, last_message_id: AtomicI32::new(0), - } + }, ChatboxEventLoop { + rx: ws_rx, + }) } - pub async fn tell(self, message: packets::client::tell::TellPacket) { + pub async fn tell(&self, message: packets::client::tell::TellPacket) { let tx = &self.tx; let packet_type = packets::client::PacketType::Tell(message); @@ -77,7 +82,7 @@ impl ChatboxClientInstance { .expect("Failed to send packet to reciever."); } - pub async fn say(self, message: packets::client::say::SayPacket) { + pub async fn say(&self, message: packets::client::say::SayPacket) { let tx = &self.tx; let packet_type = packets::client::PacketType::Say(message); @@ -92,7 +97,7 @@ impl ChatboxClientInstance { } } -impl Stream for ChatboxClientInstance { +impl Stream for ChatboxEventLoop { type Item = packets::ServerPacket; fn poll_next(