hyperlane-plugin-websocket 11.0.8

A WebSocket plugin for the Hyperlane framework, providing robust WebSocket communication capabilities and integrating with hyperlane-broadcast for efficient message dissemination.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
use crate::*;

/// Allows `String` to be used as a broadcast identifier.
impl BroadcastTypeTrait for String {}

/// Allows string slices to be used as broadcast identifiers.
impl BroadcastTypeTrait for &str {}

/// Allows `char` to be used as a broadcast identifier.
impl BroadcastTypeTrait for char {}

/// Allows `bool` to be used as a broadcast identifier.
impl BroadcastTypeTrait for bool {}

/// Allows `i8` to be used as a broadcast identifier.
impl BroadcastTypeTrait for i8 {}

/// Allows `i16` to be used as a broadcast identifier.
impl BroadcastTypeTrait for i16 {}

/// Allows `i32` to be used as a broadcast identifier.
impl BroadcastTypeTrait for i32 {}

/// Allows `i64` to be used as a broadcast identifier.
impl BroadcastTypeTrait for i64 {}

/// Allows `i128` to be used as a broadcast identifier.
impl BroadcastTypeTrait for i128 {}

/// Allows `isize` to be used as a broadcast identifier.
impl BroadcastTypeTrait for isize {}

/// Allows `u8` to be used as a broadcast identifier.
impl BroadcastTypeTrait for u8 {}

/// Allows `u16` to be used as a broadcast identifier.
impl BroadcastTypeTrait for u16 {}

/// Allows `u32` to be used as a broadcast identifier.
impl BroadcastTypeTrait for u32 {}

/// Allows `u64` to be used as a broadcast identifier.
impl BroadcastTypeTrait for u64 {}

/// Allows `u128` to be used as a broadcast identifier.
impl BroadcastTypeTrait for u128 {}

/// Allows `usize` to be used as a broadcast identifier.
impl BroadcastTypeTrait for usize {}

/// Allows `f32` to be used as a broadcast identifier.
impl BroadcastTypeTrait for f32 {}

/// Allows `f64` to be used as a broadcast identifier.
impl BroadcastTypeTrait for f64 {}

/// Allows `IpAddr` to be used as a broadcast identifier.
impl BroadcastTypeTrait for IpAddr {}

/// Allows `Ipv4Addr` to be used as a broadcast identifier.
impl BroadcastTypeTrait for Ipv4Addr {}

/// Allows `Ipv6Addr` to be used as a broadcast identifier.
impl BroadcastTypeTrait for Ipv6Addr {}

/// Allows `SocketAddr` to be used as a broadcast identifier.
impl BroadcastTypeTrait for SocketAddr {}

/// Allows `NonZeroU8` to be used as a broadcast identifier.
impl BroadcastTypeTrait for NonZeroU8 {}

/// Allows `NonZeroU16` to be used as a broadcast identifier.
impl BroadcastTypeTrait for NonZeroU16 {}

/// Allows `NonZeroU32` to be used as a broadcast identifier.
impl BroadcastTypeTrait for NonZeroU32 {}

/// Allows `NonZeroU64` to be used as a broadcast identifier.
impl BroadcastTypeTrait for NonZeroU64 {}

/// Allows `NonZeroU128` to be used as a broadcast identifier.
impl BroadcastTypeTrait for NonZeroU128 {}

/// Allows `NonZeroUsize` to be used as a broadcast identifier.
impl BroadcastTypeTrait for NonZeroUsize {}

/// Allows `NonZeroI8` to be used as a broadcast identifier.
impl BroadcastTypeTrait for NonZeroI8 {}

/// Allows `NonZeroI16` to be used as a broadcast identifier.
impl BroadcastTypeTrait for NonZeroI16 {}

/// Allows `NonZeroI32` to be used as a broadcast identifier.
impl BroadcastTypeTrait for NonZeroI32 {}

/// Allows `NonZeroI64` to be used as a broadcast identifier.
impl BroadcastTypeTrait for NonZeroI64 {}

/// Allows `NonZeroI128` to be used as a broadcast identifier.
impl BroadcastTypeTrait for NonZeroI128 {}

/// Allows `NonZeroIsize` to be used as a broadcast identifier.
impl BroadcastTypeTrait for NonZeroIsize {}

/// Allows `Infallible` to be used as a broadcast identifier.
impl BroadcastTypeTrait for Infallible {}

/// Allows references to `String` to be used as broadcast identifiers.
impl BroadcastTypeTrait for &String {}

/// Allows double references to string slices to be used as broadcast identifiers.
impl BroadcastTypeTrait for &&str {}

/// Allows references to `char` to be used as broadcast identifiers.
impl BroadcastTypeTrait for &char {}

/// Allows references to `bool` to be used as broadcast identifiers.
impl BroadcastTypeTrait for &bool {}

/// Allows references to `i8` to be used as broadcast identifiers.
impl BroadcastTypeTrait for &i8 {}

/// Allows references to `i16` to be used as broadcast identifiers.
impl BroadcastTypeTrait for &i16 {}

/// Allows references to `i32` to be used as broadcast identifiers.
impl BroadcastTypeTrait for &i32 {}

/// Allows references to `i64` to be used as broadcast identifiers.
impl BroadcastTypeTrait for &i64 {}

/// Allows references to `i128` to be used as broadcast identifiers.
impl BroadcastTypeTrait for &i128 {}

/// Allows references to `isize` to be used as broadcast identifiers.
impl BroadcastTypeTrait for &isize {}

/// Allows references to `u8` to be used as broadcast identifiers.
impl BroadcastTypeTrait for &u8 {}

/// Allows references to `u16` to be used as broadcast identifiers.
impl BroadcastTypeTrait for &u16 {}

/// Allows references to `u32` to be used as broadcast identifiers.
impl BroadcastTypeTrait for &u32 {}

/// Allows references to `u64` to be used as
/// Implements `BroadcastTypeTrait` for `&u128`.
///
/// This allows references to `u128` to be used as a broadcast identifier.
impl BroadcastTypeTrait for &u128 {}

/// Implements `BroadcastTypeTrait` for `&usize`.
///
/// This allows references to `usize` to be used as a broadcast identifier.
impl BroadcastTypeTrait for &usize {}

/// Implements `BroadcastTypeTrait` for `&f32`.
///
/// This allows references to `f32` to be used as a broadcast identifier.
impl BroadcastTypeTrait for &f32 {}

/// Implements `BroadcastTypeTrait` for `&f64`.
///
/// This allows references to `f64` to be used as a broadcast identifier.
impl BroadcastTypeTrait for &f64 {}

/// Implements `BroadcastTypeTrait` for `&IpAddr`.
///
/// This allows references to `IpAddr` to be used as a broadcast identifier.
impl BroadcastTypeTrait for &IpAddr {}

/// Implements `BroadcastTypeTrait` for `&Ipv4Addr`.
///
/// This allows references to `Ipv4Addr` to be used as a broadcast identifier.
impl BroadcastTypeTrait for &Ipv4Addr {}

/// Implements `BroadcastTypeTrait` for `&Ipv6Addr`.
///
/// This allows references to `Ipv6Addr` to be used as a broadcast identifier.
impl BroadcastTypeTrait for &Ipv6Addr {}

/// Implements `BroadcastTypeTrait` for `&SocketAddr`.
///
/// This allows references to `SocketAddr` to be used as a broadcast identifier.
impl BroadcastTypeTrait for &SocketAddr {}

/// Implements `BroadcastTypeTrait` for `&NonZeroU8`.
///
/// This allows references to `NonZeroU8` to be used as a broadcast identifier.
impl BroadcastTypeTrait for &NonZeroU8 {}

/// Implements `BroadcastTypeTrait` for `&NonZeroU16`.
///
/// This allows references to `NonZeroU16` to be used as a broadcast identifier.
impl BroadcastTypeTrait for &NonZeroU16 {}

/// Implements `BroadcastTypeTrait` for `&NonZeroU32`.
///
/// This allows references to `NonZeroU32` to be used as a broadcast identifier.
impl BroadcastTypeTrait for &NonZeroU32 {}

/// Implements `BroadcastTypeTrait` for `&NonZeroU64`.
///
/// This allows references to `NonZeroU64` to be used as a broadcast identifier.
impl BroadcastTypeTrait for &NonZeroU64 {}

/// Implements `BroadcastTypeTrait` for `&NonZeroU128`.
///
/// This allows references to `NonZeroU128` to be used as a broadcast identifier.
impl BroadcastTypeTrait for &NonZeroU128 {}

/// Implements `BroadcastTypeTrait` for `&NonZeroUsize`.
///
/// This allows references to `NonZeroUsize` to be used as a broadcast identifier.
impl BroadcastTypeTrait for &NonZeroUsize {}

/// Implements `BroadcastTypeTrait` for `&NonZeroI8`.
///
/// This allows references to `NonZeroI8` to be used as a broadcast identifier.
impl BroadcastTypeTrait for &NonZeroI8 {}

/// Implements `BroadcastTypeTrait` for `&NonZeroI16`.
///
/// This allows references to `NonZeroI16` to be used as a broadcast identifier.
impl BroadcastTypeTrait for &NonZeroI16 {}

/// Implements `BroadcastTypeTrait` for `&NonZeroI32`.
///
/// This allows references to `NonZeroI32` to be used as a broadcast identifier.
impl BroadcastTypeTrait for &NonZeroI32 {}

/// Implements `BroadcastTypeTrait` for `&NonZeroI64`.
///
/// This allows references to `NonZeroI64` to be used as a broadcast identifier.
impl BroadcastTypeTrait for &NonZeroI64 {}

/// Implements `BroadcastTypeTrait` for `&NonZeroI128`.
///
/// This allows references to `NonZeroI128` to be used as a broadcast identifier.
impl BroadcastTypeTrait for &NonZeroI128 {}

/// Implements `BroadcastTypeTrait` for `&NonZeroIsize`.
///
/// This allows references to `NonZeroIsize` to be used as a broadcast identifier.
impl BroadcastTypeTrait for &NonZeroIsize {}

/// Implements `BroadcastTypeTrait` for `&Infallible`.
///
/// This allows references to `Infallible` to be used as a broadcast identifier.
impl BroadcastTypeTrait for &Infallible {}

/// Implements the `Default` trait for `BroadcastType`.
///
/// The default value is `BroadcastType::Unknown`.
///
/// # Type Parameters
///
/// - `BroadcastTypeTrait`: The type parameter for `BroadcastType`, which must implement `BroadcastTypeTrait`.
impl<B> Default for BroadcastType<B>
where
    B: BroadcastTypeTrait,
{
    #[inline(always)]
    fn default() -> Self {
        BroadcastType::Unknown
    }
}

impl<B> BroadcastType<B>
where
    B: BroadcastTypeTrait,
{
    /// Generates a unique key string for a given broadcast type.
    ///
    /// For point-to-point types, the keys are sorted to ensure consistent key generation
    /// regardless of the order of the input keys.
    ///
    /// # Arguments
    ///
    /// - `BroadcastType<B>` - The broadcast type for which to generate the key.
    ///
    /// # Returns
    ///
    /// - `String` - The unique key string for the broadcast type.
    #[inline(always)]
    pub fn get_key(broadcast_type: BroadcastType<B>) -> String {
        match broadcast_type {
            BroadcastType::PointToPoint(key1, key2) => {
                let (first_key, second_key) = if key1 <= key2 {
                    (key1, key2)
                } else {
                    (key2, key1)
                };
                format!(
                    "{}-{}-{}",
                    POINT_TO_POINT_KEY,
                    first_key.to_string(),
                    second_key.to_string()
                )
            }
            BroadcastType::PointToGroup(key) => {
                format!("{}-{}", POINT_TO_GROUP_KEY, key.to_string())
            }
            BroadcastType::Unknown => String::new(),
        }
    }
}

impl<'a, B> WebSocketConfig<'a, B>
where
    B: BroadcastTypeTrait,
{
    /// Creates a new WebSocket configuration with the given context.
    ///
    /// # Arguments
    ///
    /// - `&mut Context` - The context object to associate with the WebSocket.
    ///
    /// # Returns
    ///
    /// - `WebSocketConfig<B>` - A new WebSocket configuration instance.
    #[inline(always)]
    pub fn new(context: &'a mut Context) -> Self {
        Self {
            context,
            capacity: DEFAULT_BROADCAST_SENDER_CAPACITY,
            broadcast_type: BroadcastType::default(),
            connected_hook: default_server_hook_handler(),
            request_hook: default_server_hook_handler(),
            sended_hook: default_server_hook_handler(),
            closed_hook: default_server_hook_handler(),
        }
    }
}

impl<'a, B> WebSocketConfig<'a, B>
where
    B: BroadcastTypeTrait,
{
    /// Sets the capacity for the broadcast sender.
    ///
    /// # Arguments
    ///
    /// - `Capacity` - The desired capacity.
    ///
    /// # Returns
    ///
    /// - `WebSocketConfig<B>` - The modified WebSocket configuration instance.
    #[inline(always)]
    pub fn set_capacity(mut self, capacity: Capacity) -> Self {
        self.capacity = capacity;
        self
    }

    /// Sets the context for the WebSocket connection.
    ///
    /// # Arguments
    ///
    /// - `&mut Context` - The context object to associate with the WebSocket.
    ///
    /// # Returns
    ///
    /// - `WebSocketConfig<B>` - The modified WebSocket configuration instance.
    #[inline(always)]
    pub fn set_context(mut self, context: &'a mut Context) -> Self {
        self.context = context;
        self
    }

    /// Sets the broadcast type for the WebSocket connection.
    ///
    /// # Arguments
    ///
    /// - `BroadcastType<B>` - The broadcast type to use for this WebSocket.
    ///
    /// # Returns
    ///
    /// - `WebSocketConfig<B>` - The modified WebSocket configuration instance.
    #[inline(always)]
    pub fn set_broadcast_type(mut self, broadcast_type: BroadcastType<B>) -> Self {
        self.broadcast_type = broadcast_type;
        self
    }

    /// Retrieves a reference to the context associated with this configuration.
    ///
    /// # Returns
    ///
    /// - `&mut Context` - A reference to the context object.
    #[inline(always)]
    pub fn get_context(&mut self) -> &mut Context {
        self.context
    }

    /// Retrieves the capacity configured for the broadcast sender.
    ///
    /// # Returns
    ///
    /// - `Capacity` - The capacity.
    #[inline(always)]
    pub fn get_capacity(&self) -> Capacity {
        self.capacity
    }

    /// Retrieves a reference to the broadcast type configured for this WebSocket.
    ///
    /// # Returns
    ///
    /// - `&BroadcastType<B>` - A reference to the broadcast type object.
    #[inline(always)]
    pub fn get_broadcast_type(&self) -> &BroadcastType<B> {
        &self.broadcast_type
    }

    /// Sets the connected hook handler.
    ///
    /// This hook is executed when the WebSocket connection is established.
    ///
    /// # Type Parameters
    ///
    /// - `S`: The hook type, which must implement `ServerHook`.
    ///
    /// # Returns
    ///
    /// The modified `WebSocketConfig` instance.
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// struct MyConnectedHook;
    /// impl ServerHook for MyConnectedHook {
    ///     async fn new(_ctx: &Context) -> Self { Self }
    ///     async fn handle(self, ctx: &Context) { /* ... */ }
    /// }
    ///
    /// let config = WebSocketConfig::new()
    ///     .set_connected_hook::<MyConnectedHook>();
    /// ```
    #[inline(always)]
    pub fn set_connected_hook<S>(mut self) -> Self
    where
        S: ServerHook,
    {
        self.connected_hook = server_hook_factory::<S>();
        self
    }

    /// Sets the request hook handler.
    ///
    /// This hook is executed when a new request is received on the WebSocket.
    ///
    /// # Type Parameters
    ///
    /// - `S`: The hook type, which must implement `ServerHook`.
    ///
    /// # Returns
    ///
    /// The modified `WebSocketConfig` instance.
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// struct MyRequestHook;
    /// impl ServerHook for MyRequestHook {
    ///     async fn new(_ctx: &Context) -> Self { Self }
    ///     async fn handle(self, ctx: &Context) { /* ... */ }
    /// }
    ///
    /// let config = WebSocketConfig::new()
    ///     .set_request_hook::<MyRequestHook>();
    /// ```
    #[inline(always)]
    pub fn set_request_hook<S>(mut self) -> Self
    where
        S: ServerHook,
    {
        self.request_hook = server_hook_factory::<S>();
        self
    }

    /// Sets the sended hook handler.
    ///
    /// This hook is executed after a message has been successfully sent over the WebSocket.
    ///
    /// # Type Parameters
    ///
    /// - `S`: The hook type, which must implement `ServerHook`.
    ///
    /// # Returns
    ///
    /// The modified `WebSocketConfig` instance.
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// struct MySendedHook;
    /// impl ServerHook for MySendedHook {
    ///     async fn new(_ctx: &Context) -> Self { Self }
    ///     async fn handle(self, ctx: &Context) { /* ... */ }
    /// }
    ///
    /// let config = WebSocketConfig::new()
    ///     .set_sended_hook::<MySendedHook>();
    /// ```
    #[inline(always)]
    pub fn set_sended_hook<S>(mut self) -> Self
    where
        S: ServerHook,
    {
        self.sended_hook = server_hook_factory::<S>();
        self
    }

    /// Sets the closed hook handler.
    ///
    /// This hook is executed when the WebSocket connection is closed.
    ///
    /// # Type Parameters
    ///
    /// - `S`: The hook type, which must implement `ServerHook`.
    ///
    /// # Returns
    ///
    /// The modified `WebSocketConfig` instance.
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// struct MyClosedHook;
    /// impl ServerHook for MyClosedHook {
    ///     async fn new(_ctx: &Context) -> Self { Self }
    ///     async fn handle(self, ctx: &Context) { /* ... */ }
    /// }
    ///
    /// let config = WebSocketConfig::new()
    ///     .set_closed_hook::<MyClosedHook>();
    /// ```
    #[inline(always)]
    pub fn set_closed_hook<S>(mut self) -> Self
    where
        S: ServerHook,
    {
        self.closed_hook = server_hook_factory::<S>();
        self
    }

    /// Retrieves a reference to the connected hook handler.
    ///
    /// # Returns
    ///
    /// - `&ServerHookHandler` - A reference to the connected hook handler.
    #[inline(always)]
    pub fn get_connected_hook(&self) -> &ServerHookHandler {
        &self.connected_hook
    }

    /// Retrieves a reference to the request hook handler.
    ///
    /// # Returns
    ///
    /// - `&ServerHookHandler` - A reference to the request hook handler.
    #[inline(always)]
    pub fn get_request_hook(&self) -> &ServerHookHandler {
        &self.request_hook
    }

    /// Retrieves a reference to the sended hook handler.
    ///
    /// # Returns
    ///
    /// - `&ServerHookHandler` - A reference to the sended hook handler.
    #[inline(always)]
    pub fn get_sended_hook(&self) -> &ServerHookHandler {
        &self.sended_hook
    }

    /// Retrieves a reference to the closed hook handler.
    ///
    /// # Returns
    ///
    /// - `&ServerHookHandler` - A reference to the closed hook handler.
    #[inline(always)]
    pub fn get_closed_hook(&self) -> &ServerHookHandler {
        &self.closed_hook
    }
}

impl WebSocket {
    /// Creates a new WebSocket instance.
    ///
    /// Initializes with a default broadcast map.
    ///
    /// # Returns
    ///
    /// - `WebSocket` - A new WebSocket instance.
    #[inline(always)]
    pub fn new() -> Self {
        Self::default()
    }

    /// Subscribes to a broadcast type or inserts a new one if it doesn't exist.
    ///
    /// # Type Parameters
    ///
    /// - `BroadcastTypeTrait`: The type implementing `BroadcastTypeTrait`.
    ///
    /// # Arguments
    ///
    /// - `BroadcastType<B>` - The broadcast type to subscribe to.
    /// - `Capacity` - The capacity for the broadcast sender if a new one is inserted.
    ///
    /// # Returns
    ///
    /// - `BroadcastMapReceiver<Vec<u8>>` - A broadcast map receiver for the specified broadcast type.
    #[inline(always)]
    fn subscribe_unwrap_or_insert<B>(
        &self,
        broadcast_type: BroadcastType<B>,
        capacity: Capacity,
    ) -> BroadcastMapReceiver<Vec<u8>>
    where
        B: BroadcastTypeTrait,
    {
        let key: String = BroadcastType::get_key(broadcast_type);
        self.broadcast_map.subscribe_or_insert(&key, capacity)
    }

    /// Subscribes to a point-to-point broadcast.
    ///
    /// # Type Parameters
    ///
    /// - `BroadcastTypeTrait`: The type implementing `BroadcastTypeTrait`.
    ///
    /// # Arguments
    ///
    /// - `&BroadcastTypeTrait` - The first identifier for the point-to-point communication.
    /// - `&BroadcastTypeTrait` - The second identifier for the point-to-point communication.
    /// - `Capacity` - The capacity for the broadcast sender.
    ///
    /// # Returns
    ///
    /// - `BroadcastMapReceiver<Vec<u8>>` - A broadcast map receiver for the point-to-point broadcast.
    #[inline(always)]
    fn point_to_point<B>(
        &self,
        key1: &B,
        key2: &B,
        capacity: Capacity,
    ) -> BroadcastMapReceiver<Vec<u8>>
    where
        B: BroadcastTypeTrait,
    {
        self.subscribe_unwrap_or_insert(
            BroadcastType::PointToPoint(key1.clone(), key2.clone()),
            capacity,
        )
    }

    /// Subscribes to a point-to-group broadcast.
    ///
    /// # Type Parameters
    ///
    /// - `BroadcastTypeTrait`: The type implementing `BroadcastTypeTrait`.
    ///
    /// # Arguments
    ///
    /// - `&BroadcastTypeTrait` - The identifier for the group.
    /// - `Capacity` - The capacity for the broadcast sender.
    ///
    /// # Returns
    ///
    /// - `BroadcastMapReceiver<Vec<u8>>` - A broadcast map receiver for the point-to-group broadcast.
    #[inline(always)]
    fn point_to_group<B>(&self, key: &B, capacity: Capacity) -> BroadcastMapReceiver<Vec<u8>>
    where
        B: BroadcastTypeTrait,
    {
        self.subscribe_unwrap_or_insert(BroadcastType::PointToGroup(key.clone()), capacity)
    }

    /// Retrieves the current receiver count for a given broadcast type.
    ///
    /// # Type Parameters
    ///
    /// - `BroadcastTypeTrait`: The type implementing `BroadcastTypeTrait`.
    ///
    /// # Arguments
    ///
    /// - `BroadcastType<B>` - The broadcast type for which to get the receiver count.
    ///
    /// # Returns
    ///
    /// - `ReceiverCount` - The number of active receivers for the broadcast type, or 0 if not found.
    #[inline(always)]
    pub fn receiver_count<B>(&self, broadcast_type: BroadcastType<B>) -> ReceiverCount
    where
        B: BroadcastTypeTrait,
    {
        let key: String = BroadcastType::get_key(broadcast_type);
        self.broadcast_map.receiver_count(&key).unwrap_or(0)
    }

    /// Calculates the receiver count before a connection is established.
    ///
    /// Ensures the count does not exceed the maximum allowed value minus one.
    ///
    /// # Type Parameters
    ///
    /// - `BroadcastTypeTrait`: The type implementing `BroadcastTypeTrait`.
    ///
    /// # Arguments
    ///
    /// - `BroadcastType<B>` - The broadcast type for which to get the receiver count.
    ///
    /// # Returns
    ///
    /// - `ReceiverCount` - The receiver count after the connection is established.
    #[inline(always)]
    pub fn receiver_count_before_connected<B>(
        &self,
        broadcast_type: BroadcastType<B>,
    ) -> ReceiverCount
    where
        B: BroadcastTypeTrait,
    {
        let count: ReceiverCount = self.receiver_count(broadcast_type);
        count.clamp(0, ReceiverCount::MAX - 1) + 1
    }

    /// Calculates the receiver count after a connection is closed.
    ///
    /// Ensures the count does not go below 0.
    ///
    /// # Type Parameters
    ///
    /// - `BroadcastTypeTrait`: The type implementing `BroadcastTypeTrait`.
    ///
    /// # Arguments
    ///
    /// - `BroadcastType<BroadcastTypeTrait>` - The broadcast type for which to get the receiver count.
    ///
    /// # Returns
    ///
    /// - `ReceiverCount` - The receiver count after the connection is closed.
    #[inline(always)]
    pub fn receiver_count_after_closed<B>(&self, broadcast_type: BroadcastType<B>) -> ReceiverCount
    where
        B: BroadcastTypeTrait,
    {
        let count: ReceiverCount = self.receiver_count(broadcast_type);
        count.clamp(1, ReceiverCount::MAX) - 1
    }

    /// Sends data to all active receivers for a given broadcast type.
    ///
    /// # Type Parameters
    ///
    /// - `Into<Vec<u8>>`: The type of data to send, which must be convertible to `Vec<u8>`.
    /// - `BroadcastTypeTrait`: The type implementing `BroadcastTypeTrait`.
    ///
    /// # Arguments
    ///
    /// - `BroadcastType<BroadcastTypeTrait>` - The broadcast type to which to send the data.
    /// - `Into<Vec<u8>>` - The data to send.
    ///
    /// # Returns
    ///
    /// - `Result<Option<ReceiverCount>, SendError<Vec<u8>>>` - A result indicating the success or failure of the send operation.
    #[inline(always)]
    pub fn try_send<T, B>(
        &self,
        broadcast_type: BroadcastType<B>,
        data: T,
    ) -> Result<Option<ReceiverCount>, SendError<Vec<u8>>>
    where
        T: Into<Vec<u8>>,
        B: BroadcastTypeTrait,
    {
        let key: String = BroadcastType::get_key(broadcast_type);
        self.broadcast_map.try_send(&key, data.into())
    }

    /// Sends data to all active receivers for a given broadcast type.
    ///
    /// This method panics if the send operation fails.
    ///
    /// # Type Parameters
    ///
    /// - `Into<Vec<u8>>`: The type of data to send, which must be convertible to `Vec<u8>`.
    /// - `BroadcastTypeTrait`: The type implementing `BroadcastTypeTrait`.
    ///
    /// # Arguments
    ///
    /// - `BroadcastType<BroadcastTypeTrait>` - The broadcast type to which to send the data.
    /// - `Into<Vec<u8>>` - The data to send.
    ///
    /// # Returns
    ///
    /// - `Option<ReceiverCount>` - The receiver count if the send operation succeeds.
    ///
    /// # Panics
    ///
    /// Panics if the send operation fails.
    #[inline(always)]
    pub fn send<T, B>(&self, broadcast_type: BroadcastType<B>, data: T) -> Option<ReceiverCount>
    where
        T: Into<Vec<u8>>,
        B: BroadcastTypeTrait,
    {
        self.try_send(broadcast_type, data).unwrap()
    }

    /// Runs the WebSocket connection, handling incoming requests and outgoing messages.
    ///
    /// This asynchronous function continuously monitors for new WebSocket requests
    /// and incoming broadcast messages, processing them according to the configured hooks.
    ///
    /// # Type Parameters
    ///
    /// - `BroadcastTypeTrait`: The type implementing `BroadcastTypeTrait`.
    ///
    /// # Arguments
    ///
    /// - `WebSocketConfig<BroadcastTypeTrait>` - The WebSocket configuration containing the configuration for this WebSocket instance.
    ///
    /// # Panics
    ///
    /// Panics if the context in the WebSocket configuration is not set (i.e., it's the default context).
    /// Panics if the broadcast type in the WebSocket configuration is `BroadcastType::Unknown`.
    pub async fn run<B>(&self, mut websocket_config: WebSocketConfig<'_, B>)
    where
        B: BroadcastTypeTrait,
    {
        let capacity: Capacity = websocket_config.get_capacity();
        let broadcast_type: BroadcastType<B> = websocket_config.get_broadcast_type().clone();
        let connected_hook: ServerHookHandler = websocket_config.get_connected_hook().clone();
        let sended_hook: ServerHookHandler = websocket_config.get_sended_hook().clone();
        let request_hook: ServerHookHandler = websocket_config.get_request_hook().clone();
        let closed_hook: ServerHookHandler = websocket_config.get_closed_hook().clone();
        let ctx: &mut Context = websocket_config.get_context();
        let mut receiver: Receiver<Vec<u8>> = match &broadcast_type {
            BroadcastType::PointToPoint(key1, key2) => self.point_to_point(key1, key2, capacity),
            BroadcastType::PointToGroup(key) => self.point_to_group(key, capacity),
            BroadcastType::Unknown => panic!("BroadcastType must be PointToPoint or PointToGroup"),
        };
        let key: String = BroadcastType::get_key(broadcast_type);
        connected_hook(ctx).await;
        loop {
            tokio::select! {
                request_res = ctx.ws_from_stream() => {
                    let mut is_err: bool = false;
                    if request_res.is_ok() {
                        request_hook(ctx).await;
                    } else {
                        is_err = true;
                        closed_hook(ctx).await;
                    }
                    if ctx.get_aborted() {
                        continue;
                    }
                    if ctx.get_closed() {
                        break;
                    }
                    let body: ResponseBody = ctx.get_response().get_body().clone();
                    is_err = self.broadcast_map.try_send(&key, body).is_err() || is_err;
                    sended_hook(ctx).await;
                    if is_err || ctx.get_closed() {
                        break;
                    }
                },
                msg_res = receiver.recv() => {
                    if let Ok(msg) = &msg_res {
                        if ctx.try_send_body_list_with_data(&WebSocketFrame::create_frame_list(msg)).await.is_ok() {
                            continue;
                        } else {
                            break;
                        }
                    }
                    break;
                }
            }
        }
        ctx.set_aborted(true).set_closed(true);
    }
}