Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ferrisbox"
version = "0.1.0"
version = "0.2.0"
edition = "2021"

[dependencies]
Expand All @@ -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"
29 changes: 17 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@ use tokio_tungstenite::{connect_async, tungstenite::Message};
pub mod models;
pub mod packets;

#[derive(Debug)]
pub struct ChatboxEventLoop {
rx: Receiver<packets::ServerPacket>,
}

#[derive(Debug)]
pub struct ChatboxClientInstance {
tx: Sender<packets::ClientPacket>,
rx: Receiver<packets::ServerPacket>,
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);

Expand All @@ -34,10 +38,10 @@ impl ChatboxClientInstance {
let message = message.into_text().expect("Got invalid UTF8");

if let Ok(packet) = serde_json::from_str::<packets::ServerPacket>(&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;
}
}
}
}
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -92,7 +97,7 @@ impl ChatboxClientInstance {
}
}

impl Stream for ChatboxClientInstance {
impl Stream for ChatboxEventLoop {
type Item = packets::ServerPacket;

fn poll_next(
Expand Down