Connect to aoba grpc server and retreive data
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use std::sync::RwLock;
|
||||
|
||||
use aoba::aoba_rpc_client::AobaRpcClient;
|
||||
use tonic::transport::Channel;
|
||||
use tonic_web_wasm_client::Client;
|
||||
|
||||
pub mod aoba {
|
||||
tonic::include_proto!("aoba");
|
||||
@@ -13,25 +13,24 @@ static RPC_CLIENT: RpcConnection = RpcConnection {
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct RpcConnection {
|
||||
client: RwLock<Option<AobaRpcClient<Channel>>>,
|
||||
client: RwLock<Option<AobaRpcClient<Client>>>,
|
||||
}
|
||||
|
||||
impl RpcConnection {
|
||||
pub async fn get_client(&self) -> AobaRpcClient<Channel> {
|
||||
self.ensure_client().await;
|
||||
pub fn get_client(&self) -> AobaRpcClient<Client> {
|
||||
self.ensure_client();
|
||||
return self.client.read().unwrap().clone().unwrap();
|
||||
}
|
||||
|
||||
async fn ensure_client(&self) {
|
||||
fn ensure_client(&self) {
|
||||
if self.client.read().unwrap().is_none() {
|
||||
let c = AobaRpcClient::connect("http://localhost:5000")
|
||||
.await
|
||||
.expect("Failed to connect RPC");
|
||||
let wasm_client = Client::new("http://localhost:5164".into());
|
||||
let c = AobaRpcClient::new(wasm_client);
|
||||
*self.client.write().unwrap() = Some(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_rpc_client() -> AobaRpcClient<Channel> {
|
||||
return RPC_CLIENT.get_client().await;
|
||||
pub fn get_rpc_client() -> AobaRpcClient<Client> {
|
||||
return RPC_CLIENT.get_client();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user