Restructuring tokio tasks and stdin receiver

This commit is contained in:
Dave Hrycyszyn
2024-06-11 16:50:21 +01:00
parent 443c4e1dac
commit f3bea8c62d
3 changed files with 43 additions and 33 deletions

View File

@@ -1,11 +1,8 @@
use async_trait::async_trait;
use bft_json_crdt::json_crdt::SignedOp;
use ezsockets::ClientConfig;
use std::io::BufRead;
use tokio::sync::mpsc;
use crate::utils;
pub(crate) struct WebSocketClient {
incoming_sender: mpsc::Sender<SignedOp>,
}
@@ -14,8 +11,7 @@ impl WebSocketClient {
/// Start the websocket client
pub(crate) async fn start(
incoming_sender: mpsc::Sender<SignedOp>,
// stdin_sender: mpsc::Sender<String>,
) -> tokio::task::JoinHandle<()> {
) -> ezsockets::Client<WebSocketClient> {
tracing_subscriber::fmt::init();
let config = ClientConfig::new("ws://localhost:8080/websocket");
let (handle, future) =
@@ -23,27 +19,8 @@ impl WebSocketClient {
tokio::spawn(async move {
future.await.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();
}
})
loop {}
handle
}
}