Skip to main content

Session

Struct Session 

Source
pub struct Session {
    pub ws: WebSocket,
    /* private fields */
}

Fields§

§ws: WebSocket

Implementations§

Source§

impl Session

Source

pub fn clone(&self) -> Self

Source§

impl Session

Source

pub fn from_ws(ws: WebSocket) -> Self

Source

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

Source

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}
Source

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

Source

pub async fn send<M: Method>(&self, data: &Message<M>) -> Result<()>

Source

pub async fn use_id(&self) -> u32

Source

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}
Source

pub async fn respond(&self, to: u32, val: Value) -> Result<()>

Source

pub async fn respond_error(&self, to: u32, val: Value) -> Result<()>

Source

pub async fn notify<M: Method>(&self, data: M::Request) -> Result<()>

Source

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.