a3s_boot/websocket/
hooks.rs1use super::connection::WebSocketGatewayConnection;
2use super::context::WebSocketGatewayInitContext;
3use crate::{BoxFuture, Result};
4use std::future::Future;
5
6pub trait WebSocketGatewayInitHook: Send + Sync + 'static {
8 fn after_init(&self, context: WebSocketGatewayInitContext) -> BoxFuture<'static, Result<()>>;
9}
10
11impl<F, Fut> WebSocketGatewayInitHook for F
12where
13 F: Fn(WebSocketGatewayInitContext) -> Fut + Send + Sync + 'static,
14 Fut: Future<Output = Result<()>> + Send + 'static,
15{
16 fn after_init(&self, context: WebSocketGatewayInitContext) -> BoxFuture<'static, Result<()>> {
17 Box::pin(self(context))
18 }
19}
20
21pub trait WebSocketGatewayConnectionHook: Send + Sync + 'static {
23 fn handle_connection(
24 &self,
25 connection: WebSocketGatewayConnection,
26 ) -> BoxFuture<'static, Result<()>>;
27}
28
29impl<F, Fut> WebSocketGatewayConnectionHook for F
30where
31 F: Fn(WebSocketGatewayConnection) -> Fut + Send + Sync + 'static,
32 Fut: Future<Output = Result<()>> + Send + 'static,
33{
34 fn handle_connection(
35 &self,
36 connection: WebSocketGatewayConnection,
37 ) -> BoxFuture<'static, Result<()>> {
38 Box::pin(self(connection))
39 }
40}
41
42pub trait WebSocketGatewayDisconnectHook: Send + Sync + 'static {
44 fn handle_disconnect(
45 &self,
46 connection: WebSocketGatewayConnection,
47 ) -> BoxFuture<'static, Result<()>>;
48}
49
50impl<F, Fut> WebSocketGatewayDisconnectHook for F
51where
52 F: Fn(WebSocketGatewayConnection) -> Fut + Send + Sync + 'static,
53 Fut: Future<Output = Result<()>> + Send + 'static,
54{
55 fn handle_disconnect(
56 &self,
57 connection: WebSocketGatewayConnection,
58 ) -> BoxFuture<'static, Result<()>> {
59 Box::pin(self(connection))
60 }
61}