pub trait ConnectHandler<A: Adapter, T>:
Sized
+ Clone
+ Send
+ Sync
+ 'static {
// Required method
fn call(&self, s: Arc<Socket<A>>, auth: Option<Value>);
// Provided methods
fn call_middleware<'a>(
&'a self,
_: Arc<Socket<A>>,
_: &'a Option<Value>,
) -> Pin<Box<dyn Future<Output = Result<(), Box<dyn Display + Send>>> + Send + 'a>> { ... }
fn with<M, T1>(self, middleware: M) -> impl ConnectHandler<A, T>
where M: ConnectMiddleware<A, T1> + Send + Sync + 'static,
T: Send + Sync + 'static,
T1: Send + Sync + 'static { ... }
}Expand description
Define a handler for the connect event.
It is implemented for closures with up to 16 arguments. They must implement the FromConnectParts trait.
Required Methods§
Provided Methods§
Sourcefn call_middleware<'a>(
&'a self,
_: Arc<Socket<A>>,
_: &'a Option<Value>,
) -> Pin<Box<dyn Future<Output = Result<(), Box<dyn Display + Send>>> + Send + 'a>>
fn call_middleware<'a>( &'a self, _: Arc<Socket<A>>, _: &'a Option<Value>, ) -> Pin<Box<dyn Future<Output = Result<(), Box<dyn Display + Send>>> + Send + 'a>>
Call the middleware with the given arguments.
Sourcefn with<M, T1>(self, middleware: M) -> impl ConnectHandler<A, T>
fn with<M, T1>(self, middleware: M) -> impl ConnectHandler<A, T>
Wraps this ConnectHandler with a new ConnectMiddleware.
The new provided middleware will be called before the current one.
§Example
async fn handler(s: SocketRef) {
println!("socket connected on / namespace with id: {}", s.id);
}
#[derive(Debug)]
struct AuthError;
impl std::fmt::Display for AuthError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "AuthError")
}
}
impl std::error::Error for AuthError {}
async fn middleware(s: SocketRef, Data(token): Data<String>) -> Result<(), AuthError> {
println!("second middleware called");
if token != "secret" {
Err(AuthError)
} else {
Ok(())
}
}
async fn other_middleware(s: SocketRef) -> Result<(), AuthError> {
println!("first middleware called");
if s.req_parts().uri.query().map(|q| q.contains("secret")).unwrap_or_default() {
Err(AuthError)
} else {
Ok(())
}
}
let (_, io) = SocketIo::new_layer();
io.ns("/", handler.with(middleware).with(other_middleware));Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.