pub struct Session {
pub ws: WebSocket,
/* private fields */
}Fields§
§ws: WebSocketImplementations§
Source§impl Session
impl Session
pub fn from_ws(ws: WebSocket) -> Self
Sourcepub async fn connect(addr: &str, path: &str) -> Result<Self>
pub async fn connect(addr: &str, path: &str) -> Result<Self>
Examples found in repository?
examples/client.rs (line 16)
15async fn main() -> session_rs::Result<()> {
16 let session = Session::connect("127.0.0.1:8080", "/").await?;
17
18 session.start_receiver();
19
20 println!(
21 "Hi: {:?}",
22 session
23 .request::<Data>("Hello from client".to_string())
24 .await?
25 );
26
27 println!(
28 "Invalid data response: {:?}",
29 session
30 .request::<Data>("invalid_data".to_string())
31 .await?
32 );
33
34 tokio::time::sleep(tokio::time::Duration::from_millis(1000)).await;
35
36 session.close().await?;
37 Ok(())
38}Source§impl Session
impl Session
Sourcepub fn start_receiver(&self)
pub fn start_receiver(&self)
Examples found in repository?
examples/client.rs (line 18)
15async fn main() -> session_rs::Result<()> {
16 let session = Session::connect("127.0.0.1:8080", "/").await?;
17
18 session.start_receiver();
19
20 println!(
21 "Hi: {:?}",
22 session
23 .request::<Data>("Hello from client".to_string())
24 .await?
25 );
26
27 println!(
28 "Invalid data response: {:?}",
29 session
30 .request::<Data>("invalid_data".to_string())
31 .await?
32 );
33
34 tokio::time::sleep(tokio::time::Duration::from_millis(1000)).await;
35
36 session.close().await?;
37 Ok(())
38}Sourcepub async fn on<M: Method, Fut: Future<Output = Result<M::Response, M::Error>> + Send + 'static>(
&self,
handler: impl Fn(u32, M::Request) -> Fut + Send + Sync + 'static,
)
pub async fn on<M: Method, Fut: Future<Output = Result<M::Response, M::Error>> + Send + 'static>( &self, handler: impl Fn(u32, M::Request) -> Fut + Send + Sync + 'static, )
Examples found in repository?
examples/server.rs (lines 21-29)
15async fn main() -> session_rs::Result<()> {
16 let server = SessionServer::bind("127.0.0.1:8080").await?;
17
18 server
19 .session_loop(async |session, _| {
20 session
21 .on::<Data, _>(async |_, req| {
22 println!("Msg from client: {req}");
23
24 if req == "invalid_data" {
25 return Err("Invalid data".to_string());
26 }
27
28 Ok("Hello from server".to_string())
29 })
30 .await;
31
32 Ok(())
33 })
34 .await
35}Source§impl Session
impl Session
pub async fn send<M: Method>(&self, data: &Message<M>) -> Result<()>
pub async fn use_id(&self) -> u32
Sourcepub async fn request<M: Method>(
&self,
req: M::Request,
) -> Result<Result<M::Response, M::Error>>
pub async fn request<M: Method>( &self, req: M::Request, ) -> Result<Result<M::Response, M::Error>>
Examples found in repository?
examples/client.rs (line 23)
15async fn main() -> session_rs::Result<()> {
16 let session = Session::connect("127.0.0.1:8080", "/").await?;
17
18 session.start_receiver();
19
20 println!(
21 "Hi: {:?}",
22 session
23 .request::<Data>("Hello from client".to_string())
24 .await?
25 );
26
27 println!(
28 "Invalid data response: {:?}",
29 session
30 .request::<Data>("invalid_data".to_string())
31 .await?
32 );
33
34 tokio::time::sleep(tokio::time::Duration::from_millis(1000)).await;
35
36 session.close().await?;
37 Ok(())
38}pub async fn respond(&self, to: u32, val: Value) -> Result<()>
pub async fn respond_error(&self, to: u32, val: Value) -> Result<()>
pub async fn notify<M: Method>(&self, data: M::Request) -> Result<()>
Sourcepub async fn close(&self) -> Result<()>
pub async fn close(&self) -> Result<()>
Examples found in repository?
examples/client.rs (line 36)
15async fn main() -> session_rs::Result<()> {
16 let session = Session::connect("127.0.0.1:8080", "/").await?;
17
18 session.start_receiver();
19
20 println!(
21 "Hi: {:?}",
22 session
23 .request::<Data>("Hello from client".to_string())
24 .await?
25 );
26
27 println!(
28 "Invalid data response: {:?}",
29 session
30 .request::<Data>("invalid_data".to_string())
31 .await?
32 );
33
34 tokio::time::sleep(tokio::time::Duration::from_millis(1000)).await;
35
36 session.close().await?;
37 Ok(())
38}Auto Trait Implementations§
impl Freeze for Session
impl !RefUnwindSafe for Session
impl Send for Session
impl Sync for Session
impl Unpin for Session
impl UnsafeUnpin for Session
impl !UnwindSafe for Session
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more