Session types

This commit is contained in:
2026-02-19 02:26:32 +01:00
parent f7f1e97671
commit 0253f15931
2 changed files with 76 additions and 22 deletions
+22 -22
View File
@@ -1,32 +1,32 @@
use std::sync::Arc;
use serde::{Deserialize, Serialize};
use session_rs::session::Session;
use session_rs::ws::{Frame, WebSocket};
#[derive(Debug, Serialize, Deserialize)]
struct Data {}
type Communication = Session<Data, Data, Data, Data, Data>;
#[tokio::main(flavor = "current_thread")]
async fn main() -> session_rs::Result<()> {
let session = Arc::new(WebSocket::connect("127.0.0.1:8080", "/").await?);
let session = Communication::connect("127.0.0.1:8080", "/").await?;
// Spawn read loop
let read_session = Arc::clone(&session);
tokio::spawn(async move {
loop {
match read_session.read().await {
Ok(Frame::Text(text)) => {
println!("Server says: {}", text);
}
Ok(_) => {}
Err(_) => break,
}
}
});
// tokio::spawn({
// let session = session.clone();
// async move {
// loop {
// match session.read().await {
// Ok(Frame::Text(text)) => {
// println!("Server says: {}", text);
// }
// Ok(_) => {}
// Err(_) => break,
// }
// }
// }
// });
// Send a few messages
for i in 0..5 {
println!("sending");
let msg = serde_json::json!({ "hello": i });
session.send(&msg.to_string()).await?;
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
}
session.request(&Data {}).await?;
session.close().await?;
Ok(())