Using the ezsockets call methods to shoot text at the websocket.
This commit is contained in:
@@ -5,7 +5,6 @@ use tokio::sync::mpsc;
|
||||
|
||||
pub(crate) struct WebSocketClient {
|
||||
incoming_sender: mpsc::Sender<SignedOp>,
|
||||
network_receiver: mpsc::Receiver<SignedOp>,
|
||||
handle: ezsockets::Client<WebSocketClient>,
|
||||
}
|
||||
|
||||
@@ -13,14 +12,12 @@ impl WebSocketClient {
|
||||
/// Start the websocket client
|
||||
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,
|
||||
network_receiver,
|
||||
handle: client,
|
||||
},
|
||||
config,
|
||||
@@ -31,18 +28,6 @@ impl WebSocketClient {
|
||||
});
|
||||
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]
|
||||
@@ -50,9 +35,9 @@ impl ezsockets::ClientExt for WebSocketClient {
|
||||
type Call = String;
|
||||
|
||||
async fn on_text(&mut self, text: String) -> Result<(), ezsockets::Error> {
|
||||
tracing::info!("received text: {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();
|
||||
self.incoming_sender.send(incoming).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -62,8 +47,9 @@ impl ezsockets::ClientExt for WebSocketClient {
|
||||
}
|
||||
|
||||
async fn on_call(&mut self, call: Self::Call) -> Result<(), ezsockets::Error> {
|
||||
println!("received call: {call}");
|
||||
self.start().await;
|
||||
let to_send = serde_json::to_string(&call).unwrap();
|
||||
tracing::info!("sending signed op: {to_send:?}");
|
||||
self.handle.text(to_send)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user