Skip to main content

hyperlane_plugin_websocket/
struct.rs

1use super::*;
2
3/// Represents a WebSocket instance.
4///
5/// This struct manages broadcast capabilities and holds the internal broadcast map
6/// responsible for handling message distribution to various WebSocket connections.
7#[derive(Clone, Data, Debug, Default)]
8pub struct WebSocket {
9    /// The internal broadcast map.
10    ///
11    /// This map is used for managing WebSocket message distribution.
12    pub(crate) broadcast_map: BroadcastMap<Vec<u8>>,
13}
14
15/// Process-wide [`WebSocketPlugin`] singleton holding a shared [`WebSocket`] room hub.
16#[derive(Clone, Debug, Default)]
17pub struct WebSocketPlugin {
18    /// Shared WebSocket broadcast hub.
19    pub(crate) websocket: WebSocket,
20}
21
22/// Configuration for a WebSocket connection.
23///
24/// This struct encapsulates all necessary parameters for setting up and managing
25/// a WebSocket connection, including context, buffer sizes, capacity, broadcast type,
26/// and hook handlers for different lifecycle events.
27///
28/// # Type Parameters
29///
30/// - `B`: The type used for broadcast keys, which must implement `BroadcastTypeTrait`.
31#[derive(Data)]
32pub struct WebSocketConfig<'a, B: BroadcastTypeTrait> {
33    /// The underlying stream for this WebSocket connection.
34    #[set(skip)]
35    pub(crate) stream: &'a mut Stream,
36    /// The Hyperlane context.
37    ///
38    /// This context is associated with this WebSocket connection.
39    #[set(skip)]
40    pub(crate) context: &'a mut Context,
41    /// The capacity.
42    ///
43    /// This is the capacity of the broadcast sender channel.
44    #[get(type(copy))]
45    #[set(skip)]
46    pub(crate) capacity: Capacity,
47    /// The broadcast type.
48    ///
49    /// This defines the type of broadcast this WebSocket connection will participate in
50    /// (point-to-point or point-to-group).
51    #[set(skip)]
52    pub(crate) broadcast_type: BroadcastType<B>,
53    /// The connected hook handler.
54    ///
55    /// This hook is executed when the WebSocket connection is established.
56    #[set(skip)]
57    pub(crate) connected_hook: ServerHookHandler,
58    /// The request hook handler.
59    ///
60    /// This hook is executed when a new request is received on the WebSocket.
61    #[set(skip)]
62    pub(crate) request_hook: ServerHookHandler,
63    /// The sended hook handler.
64    ///
65    /// This hook is executed after a message has been successfully sent over the WebSocket.
66    #[set(skip)]
67    pub(crate) sended_hook: ServerHookHandler,
68    /// The closed hook handler.
69    ///
70    /// This hook is executed when the WebSocket connection is closed.
71    #[set(skip)]
72    pub(crate) closed_hook: ServerHookHandler,
73}