Simplifying

This commit is contained in:
Dave Hrycyszyn
2024-06-10 16:43:45 +01:00
parent 6077c3a519
commit 443c4e1dac
3 changed files with 51 additions and 28 deletions

View File

@@ -4,19 +4,18 @@ use ezsockets::ClientConfig;
use std::io::BufRead;
use tokio::sync::mpsc;
use crate::{node::SideNode, utils};
use crate::utils;
pub(crate) struct WebSocketClient {
incoming_sender: mpsc::Sender<SignedOp>,
}
impl WebSocketClient {
pub(crate) fn new(incoming_sender: mpsc::Sender<SignedOp>) -> Self {
Self { incoming_sender }
}
/// Start the websocket client
pub(crate) async fn start(node: &mut SideNode, incoming_sender: mpsc::Sender<SignedOp>) {
pub(crate) async fn start(
incoming_sender: mpsc::Sender<SignedOp>,
// stdin_sender: mpsc::Sender<String>,
) -> tokio::task::JoinHandle<()> {
tracing_subscriber::fmt::init();
let config = ClientConfig::new("ws://localhost:8080/websocket");
let (handle, future) =
@@ -24,24 +23,27 @@ impl WebSocketClient {
tokio::spawn(async move {
future.await.unwrap();
});
let stdin = std::io::stdin();
let lines = stdin.lock().lines();
for line in lines {
println!("We don't get here until we send a message");
let line = line.unwrap();
let signed_op = if let "exit" = line.as_str() {
break;
} else if let "trace" = line.as_str() {
node.trace_crdt();
continue;
} else {
let fake = utils::fake_transaction("foo123".to_string());
node.add_transaction_local(fake)
};
tracing::info!("sending {:?}", signed_op);
let json = serde_json::to_string(&signed_op).unwrap();
handle.text(json).unwrap();
}
tokio::spawn(async move {
let stdin = std::io::stdin();
let lines = stdin.lock().lines();
for line in lines {
println!("We don't get here until we send a message");
let line = line.unwrap();
let signed_op = if let "exit" = line.as_str() {
break;
// } else if let "trace" = line.as_str() {
// node.trace_crdt();
// continue;
} else {
let fake = utils::fake_transaction("foo123".to_string());
// stdin_sender.send(fake).await.unwrap();
fake
};
tracing::info!("sending {:?}", signed_op);
let json = serde_json::to_string(&signed_op).unwrap();
handle.text(json).unwrap();
}
})
}
}