use async_trait::async_trait; use bft_json_crdt::json_crdt::SignedOp; use ezsockets::ClientConfig; use tokio::sync::mpsc; pub(crate) struct WebSocketClient { incoming_sender: mpsc::Sender, } impl WebSocketClient { /// Start the websocket client pub(crate) async fn start( incoming_sender: mpsc::Sender, ) -> ezsockets::Client { tracing_subscriber::fmt::init(); let config = ClientConfig::new("ws://localhost:8080/websocket"); let (handle, future) = ezsockets::connect(|_client| WebSocketClient { incoming_sender }, config).await; tokio::spawn(async move { future.await.unwrap(); }); loop {} handle } } #[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(); tracing::info!("received signed op: {incoming:?}"); self.incoming_sender.send(incoming).await.unwrap(); Ok(()) } async fn on_binary(&mut self, bytes: Vec) -> Result<(), ezsockets::Error> { tracing::info!("received bytes: {bytes:?}"); Ok(()) } async fn on_call(&mut self, call: Self::Call) -> Result<(), ezsockets::Error> { let () = call; Ok(()) } }