Files
bft-crdt-experiment/side-node/src/websocket/mod.rs

61 lines
1.7 KiB
Rust
Raw Normal View History

2024-06-06 19:25:54 +01:00
/*
loop {
let author = general_purpose::STANDARD.encode(&incoming_operation.author());
bft_crdt.apply(incoming_operation.clone());
}
2024-06-06 19:25:54 +01:00
*/
2024-06-06 19:32:29 +01:00
use async_trait::async_trait;
use bft_json_crdt::json_crdt::BaseCrdt;
use ezsockets::ClientConfig;
use fastcrypto::ed25519::Ed25519KeyPair;
use std::io::BufRead;
use crate::list_transaction_crdt::{self, CrdtList};
2024-06-06 19:32:29 +01:00
struct Client {}
#[async_trait]
impl ezsockets::ClientExt for Client {
type Call = ();
async fn on_text(&mut self, text: String) -> Result<(), ezsockets::Error> {
tracing::info!("received message: {text}");
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(keys: Ed25519KeyPair, bft_crdt: &mut BaseCrdt<CrdtList>) {
tracing_subscriber::fmt::init();
let config = ClientConfig::new("ws://localhost:8080/websocket");
let (handle, future) = ezsockets::connect(|_client| Client {}, 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();
2024-06-06 19:32:29 +01:00
}
}