Improved examples

This commit is contained in:
2026-02-19 05:17:34 +01:00
parent d1bed32ca3
commit 2da9c1abed
2 changed files with 29 additions and 15 deletions
+14 -10
View File
@@ -6,9 +6,9 @@ struct Data;
impl Method for Data { impl Method for Data {
const NAME: &'static str = "data"; const NAME: &'static str = "data";
type Request = (); type Request = String;
type Response = (); type Response = String;
type Error = (); type Error = String;
} }
#[tokio::main(flavor = "current_thread")] #[tokio::main(flavor = "current_thread")]
@@ -17,15 +17,19 @@ async fn main() -> session_rs::Result<()> {
session.start_receiver(); session.start_receiver();
println!("{:?}", session.request::<Data>(()).await?); println!(
"Hi: {:?}",
session session
.on::<Data, _>(async |i, d| { .request::<Data>("Hello from client".to_string())
println!("Ok {i} {d:?}"); .await?
);
Ok(()) println!(
}) "Invalid data response: {:?}",
.await; session
.request::<Data>("invalid_data".to_string())
.await?
);
tokio::time::sleep(tokio::time::Duration::from_millis(1000)).await; tokio::time::sleep(tokio::time::Duration::from_millis(1000)).await;
+14 -4
View File
@@ -8,9 +8,9 @@ struct Data;
impl Method for Data { impl Method for Data {
const NAME: &'static str = "data"; const NAME: &'static str = "data";
type Request = (); type Request = String;
type Response = (); type Response = String;
type Error = (); type Error = String;
} }
#[tokio::main(flavor = "current_thread")] #[tokio::main(flavor = "current_thread")]
@@ -32,7 +32,17 @@ async fn main() -> session_rs::Result<()> {
session.start_receiver(); session.start_receiver();
session.on::<Data, _>(async |_, _| Ok(())).await; session
.on::<Data, _>(async |_, req| {
println!("Msg from client: {req}");
if req == "invalid_data" {
return Err("Invalid data".to_string());
}
Ok("Hello from server".to_string())
})
.await;
}); });
} }
} }