Skip to main content

Module disconnect

Module disconnect 

Source
Expand description

DisconnectHandler trait and implementations, used to handle the disconnect event. It has a flexible axum-like API, you can put any arguments as long as it implements the FromDisconnectParts trait.

You can also implement the FromDisconnectParts trait for your own types. See the extract module doc for more details on available extractors.

Handlers must be async.

§Example with async closures

let (svc, io) = SocketIo::new_svc();
io.ns("/", async |s: SocketRef| {
    s.on_disconnect(async |s: SocketRef| {
        println!("Socket {} was disconnected", s.id);
    });
});

§Example with an async non-anonymous functions

async fn handler(s: SocketRef, reason: DisconnectReason) {
    tokio::time::sleep(std::time::Duration::from_secs(1)).await;
    println!("Socket disconnected on {} namespace with id and reason: {} {}", s.ns(), s.id, reason);
}

let (svc, io) = SocketIo::new_svc();

// You can reuse the same handler for multiple sockets
io.ns("/", async |s: SocketRef| {
    s.on_disconnect(handler);
});
io.ns("/admin", async |s: SocketRef| {
    s.on_disconnect(handler);
});

Traits§

DisconnectHandler
Define a handler for the disconnect event. It is implemented for closures with up to 16 arguments. They must implement the FromDisconnectParts trait.
FromDisconnectParts
A trait used to extract the arguments from the disconnect event. The Result associated type is used to return an error if the extraction fails, in this case the DisconnectHandler is not called.