ServerEventCallbacks

Struct ServerEventCallbacks 

Source
pub struct ServerEventCallbacks<R>
where R: DeserializeOwned + 'static,
{ /* private fields */ }
Expand description

Configuration for a server’s event callbacks.

§Events

There are four events for which callbacks can be registered:

  • connect
  • disconnect
  • receive
  • stop

All callbacks are optional, and can be registered for any combination of these events. Note that each callback must be provided as a function or closure returning a thread-safe future. The future will be awaited by the runtime.

§Example


let server = Server::builder()
    .sending::<usize>()
    .receiving::<String>()
    .with_event_callbacks(
        ServerEventCallbacks::new()
            .on_connect(move |client_id| async move {
                // some async operation...
                println!("Client with ID {} connected", client_id);
            })
            .on_disconnect(move |client_id| async move {
                // some async operation...
                println!("Client with ID {} disconnected", client_id);
            })
            .on_receive(move |client_id, data| async move {
                // some async operation...
                println!("Received data from client with ID {}: {}", client_id, data);
            })
            .on_stop(move || async move {
                // some async operation...
                println!("Server closed");
            })
    )
    .start(("127.0.0.1", 29275))
    .await
    .unwrap();

Implementations§

Source§

impl<R> ServerEventCallbacks<R>
where R: DeserializeOwned + 'static,

Source

pub const fn new() -> Self

Creates a new server event callbacks configuration with all callbacks empty.

Source

pub fn on_connect<C, F>(self, callback: C) -> Self
where C: Fn(usize) -> F + Send + Sync + 'static, F: Future<Output = ()> + Send + 'static,

Registers a callback on the connect event.

Source

pub fn on_disconnect<C, F>(self, callback: C) -> Self
where C: Fn(usize) -> F + Send + Sync + 'static, F: Future<Output = ()> + Send + 'static,

Registers a callback on the disconnect event.

Source

pub fn on_receive<C, F>(self, callback: C) -> Self
where C: Fn(usize, R) -> F + Send + Sync + 'static, F: Future<Output = ()> + Send + 'static,

Registers a callback on the receive event.

Source

pub fn on_stop<C, F>(self, callback: C) -> Self
where C: Fn() -> F + Send + Sync + 'static, F: Future<Output = ()> + Send + 'static,

Registers a callback on the stop event.

Trait Implementations§

Source§

impl<R> Default for ServerEventCallbacks<R>
where R: DeserializeOwned + 'static,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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.