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,
impl<R> ServerEventCallbacks<R>where
R: DeserializeOwned + 'static,
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Creates a new server event callbacks configuration with all callbacks empty.
Sourcepub fn on_connect<C, F>(self, callback: C) -> Self
pub fn on_connect<C, F>(self, callback: C) -> Self
Registers a callback on the connect
event.
Sourcepub fn on_disconnect<C, F>(self, callback: C) -> Self
pub fn on_disconnect<C, F>(self, callback: C) -> Self
Registers a callback on the disconnect
event.
Sourcepub fn on_receive<C, F>(self, callback: C) -> Self
pub fn on_receive<C, F>(self, callback: C) -> Self
Registers a callback on the receive
event.
Trait Implementations§
Source§impl<R> Default for ServerEventCallbacks<R>where
R: DeserializeOwned + 'static,
impl<R> Default for ServerEventCallbacks<R>where
R: DeserializeOwned + 'static,
Auto Trait Implementations§
impl<R> Freeze for ServerEventCallbacks<R>
impl<R> !RefUnwindSafe for ServerEventCallbacks<R>
impl<R> Send for ServerEventCallbacks<R>
impl<R> Sync for ServerEventCallbacks<R>
impl<R> Unpin for ServerEventCallbacks<R>
impl<R> !UnwindSafe for ServerEventCallbacks<R>
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