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
+15 -11
View File
@@ -6,9 +6,9 @@ struct Data;
impl Method for Data {
const NAME: &'static str = "data";
type Request = ();
type Response = ();
type Error = ();
type Request = String;
type Response = String;
type Error = String;
}
#[tokio::main(flavor = "current_thread")]
@@ -17,15 +17,19 @@ async fn main() -> session_rs::Result<()> {
session.start_receiver();
println!("{:?}", session.request::<Data>(()).await?);
println!(
"Hi: {:?}",
session
.request::<Data>("Hello from client".to_string())
.await?
);
session
.on::<Data, _>(async |i, d| {
println!("Ok {i} {d:?}");
Ok(())
})
.await;
println!(
"Invalid data response: {:?}",
session
.request::<Data>("invalid_data".to_string())
.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 {
const NAME: &'static str = "data";
type Request = ();
type Response = ();
type Error = ();
type Request = String;
type Response = String;
type Error = String;
}
#[tokio::main(flavor = "current_thread")]
@@ -32,7 +32,17 @@ async fn main() -> session_rs::Result<()> {
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;
});
}
}