diff options
author | Ilion Beyst <ilion.beyst@gmail.com> | 2022-06-05 21:22:38 +0200 |
---|---|---|
committer | Ilion Beyst <ilion.beyst@gmail.com> | 2022-06-05 21:22:38 +0200 |
commit | 90ecb13a1772dfdab20a006b421102c0aa584f60 (patch) | |
tree | bfcbec37f204e58a0abcfa210945d54ed60d9dd1 /planetwars-client | |
parent | c3d32e051cfeb1deffffbdfe533d17736f72aeda (diff) | |
download | planetwars.dev-90ecb13a1772dfdab20a006b421102c0aa584f60.tar.xz planetwars.dev-90ecb13a1772dfdab20a006b421102c0aa584f60.zip |
baby steps towards a working bot api
Diffstat (limited to 'planetwars-client')
-rw-r--r-- | planetwars-client/Cargo.toml | 1 | ||||
-rw-r--r-- | planetwars-client/src/main.rs | 32 |
2 files changed, 24 insertions, 9 deletions
diff --git a/planetwars-client/Cargo.toml b/planetwars-client/Cargo.toml index 10de887..52c3c64 100644 --- a/planetwars-client/Cargo.toml +++ b/planetwars-client/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" [dependencies] tokio = { version = "1.15", features = ["full"] } +tokio-stream = "0.1.9" prost = "0.10" tonic = "0.7.2" diff --git a/planetwars-client/src/main.rs b/planetwars-client/src/main.rs index 9d9bdab..d995ebc 100644 --- a/planetwars-client/src/main.rs +++ b/planetwars-client/src/main.rs @@ -2,20 +2,34 @@ pub mod pb { tonic::include_proto!("grpc.planetwars.bot_api"); } -use pb::test_service_client::TestServiceClient; -use pb::{Hello, HelloResponse}; -use tonic::Response; +use pb::bot_api_service_client::BotApiServiceClient; +use tokio_stream::wrappers::UnboundedReceiverStream; + +use tokio::sync::mpsc; #[tokio::main] async fn main() { - let mut client = TestServiceClient::connect("http://localhost:50051") + let mut client = BotApiServiceClient::connect("http://localhost:50051") .await .unwrap(); - let response: Response<HelloResponse> = client - .greet(Hello { - hello_message: "robbe".to_string(), - }) + + let (tx, rx) = mpsc::unbounded_channel(); + let mut stream = client + .connect_bot(UnboundedReceiverStream::new(rx)) .await + .unwrap() + .into_inner(); + while let Some(message) = stream.message().await.unwrap() { + let state = String::from_utf8(message.content).unwrap(); + println!("{}", state); + let response = r#"{ moves: [] }"#; + tx.send(pb::PlayerRequestResponse { + request_id: message.request_id, + content: response.as_bytes().to_vec(), + }) .unwrap(); - println!("{}", response.get_ref().response); + } + std::mem::drop(tx); + // for clean exit + std::mem::drop(client); } |