Skip to main content

a3s_boot/websocket/
hooks.rs

1use super::connection::WebSocketGatewayConnection;
2use super::context::WebSocketGatewayInitContext;
3use crate::{BoxFuture, Result};
4use std::future::Future;
5
6/// Hook invoked when a WebSocket gateway is initialized during application bootstrap.
7pub 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
21/// Hook invoked when a WebSocket client connects to a gateway.
22pub 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
42/// Hook invoked when a WebSocket client disconnects from a gateway.
43pub 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}