Figured out what on_call is for
This commit is contained in:
@@ -5,31 +5,51 @@ use tokio::sync::mpsc;
|
||||
|
||||
pub(crate) struct WebSocketClient {
|
||||
incoming_sender: mpsc::Sender<SignedOp>,
|
||||
network_receiver: mpsc::Receiver<SignedOp>,
|
||||
handle: ezsockets::Client<WebSocketClient>,
|
||||
}
|
||||
|
||||
impl WebSocketClient {
|
||||
/// Start the websocket client
|
||||
pub(crate) async fn start(
|
||||
pub(crate) async fn new(
|
||||
incoming_sender: mpsc::Sender<SignedOp>,
|
||||
network_receiver: mpsc::Receiver<SignedOp>,
|
||||
) -> ezsockets::Client<WebSocketClient> {
|
||||
tracing_subscriber::fmt::init();
|
||||
let config = ClientConfig::new("ws://localhost:8080/websocket");
|
||||
let (handle, future) =
|
||||
ezsockets::connect(|_client| WebSocketClient { incoming_sender }, config).await;
|
||||
let (handle, future) = ezsockets::connect(
|
||||
|client| WebSocketClient {
|
||||
incoming_sender,
|
||||
network_receiver,
|
||||
handle: client,
|
||||
},
|
||||
config,
|
||||
)
|
||||
.await;
|
||||
tokio::spawn(async move {
|
||||
future.await.unwrap();
|
||||
});
|
||||
loop {}
|
||||
handle
|
||||
}
|
||||
|
||||
pub(crate) async fn start(&mut self) {
|
||||
loop {
|
||||
match self.network_receiver.try_recv() {
|
||||
Ok(signed_op) => {
|
||||
let to_send = serde_json::to_string(&signed_op).unwrap();
|
||||
self.handle.text(to_send).unwrap();
|
||||
}
|
||||
Err(_) => {} // ignore empty channel errors in this PoC
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl ezsockets::ClientExt for WebSocketClient {
|
||||
type Call = ();
|
||||
type Call = String;
|
||||
|
||||
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();
|
||||
@@ -42,7 +62,8 @@ impl ezsockets::ClientExt for WebSocketClient {
|
||||
}
|
||||
|
||||
async fn on_call(&mut self, call: Self::Call) -> Result<(), ezsockets::Error> {
|
||||
let () = call;
|
||||
println!("received call: {call}");
|
||||
self.start().await;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user