Files
bft-crdt-experiment/side-node/src/websocket.rs
Dave Hrycyszyn 5d6a1e806a Nearly there
2024-06-07 17:35:38 +01:00

60 lines
1.9 KiB
Rust

use async_trait::async_trait;
use ezsockets::ClientConfig;
use std::io::BufRead;
pub(crate) struct WebSocketClient {}
impl WebSocketClient {
pub(crate) fn new() -> Self {
Self {}
}
/// Start the websocket client
pub(crate) async fn start(&mut self) {
tracing_subscriber::fmt::init();
let config = ClientConfig::new("ws://localhost:8080/websocket");
let (handle, future) = ezsockets::connect(|_client| WebSocketClient {}, config).await;
tokio::spawn(async move {
future.await.unwrap();
});
let stdin = std::io::stdin();
let lines = stdin.lock().lines();
for line in lines {
let line = line.unwrap();
let signed_op = if let "exit" = line.as_str() {
break;
} else {
// list_transaction_crdt::create(bft_crdt, &keys)
};
tracing::info!("sending {:?}", signed_op);
let json = serde_json::to_string(&signed_op).unwrap();
handle.text(json).unwrap();
}
}
}
#[async_trait]
impl ezsockets::ClientExt for WebSocketClient {
type Call = ();
async fn on_text(&mut self, text: String) -> Result<(), ezsockets::Error> {
tracing::info!("received message: {text}");
// let incoming: bft_json_crdt::json_crdt::SignedOp = serde_json::from_str(&text).unwrap();
// let author = base64::engine::general_purpose::STANDARD.encode(&incoming.author());
// self.bft_crdt.apply(incoming.clone());
Ok(())
}
async fn on_binary(&mut self, bytes: Vec<u8>) -> Result<(), ezsockets::Error> {
tracing::info!("received bytes: {bytes:?}");
Ok(())
}
async fn on_call(&mut self, call: Self::Call) -> Result<(), ezsockets::Error> {
let () = call;
Ok(())
}
}
// pub(crate) async fn start(self, keys: Ed25519KeyPair, bft_crdt: &mut BaseCrdt<CrdtList>) {}