Skip to main content

a3s_boot/websocket/
server.rs

1use super::gateway::WebSocketGatewayDefinition;
2use super::message::WebSocketMessage;
3use crate::Result;
4use std::fmt;
5
6/// Adapter-neutral gateway server handle for broadcasting and connection lookup.
7#[derive(Clone)]
8pub struct WebSocketGatewayServer {
9    gateway: WebSocketGatewayDefinition,
10}
11
12impl WebSocketGatewayServer {
13    pub(crate) fn new(gateway: WebSocketGatewayDefinition) -> Self {
14        Self { gateway }
15    }
16
17    pub fn path(&self) -> &str {
18        self.gateway.path()
19    }
20
21    pub fn namespace(&self) -> Option<&str> {
22        self.gateway.namespace()
23    }
24
25    pub fn events(&self) -> Vec<&str> {
26        self.gateway.events()
27    }
28
29    pub fn active_connection_count(&self) -> Result<usize> {
30        self.gateway.active_connection_count()
31    }
32
33    pub fn active_connection_ids(&self) -> Result<Vec<u64>> {
34        self.gateway.active_connection_ids()
35    }
36
37    pub fn rooms(&self) -> Result<Vec<String>> {
38        self.gateway.rooms()
39    }
40
41    pub fn room_members(&self, room: impl Into<String>) -> Result<Vec<u64>> {
42        self.gateway.room_members(room)
43    }
44
45    pub async fn emit_to_connection(
46        &self,
47        connection_id: u64,
48        message: WebSocketMessage,
49    ) -> Result<bool> {
50        self.gateway
51            .emit_to_connection(connection_id, message)
52            .await
53    }
54
55    pub async fn broadcast(&self, message: WebSocketMessage) -> Result<usize> {
56        self.gateway.broadcast(message).await
57    }
58
59    pub async fn broadcast_to_room(
60        &self,
61        room: impl Into<String>,
62        message: WebSocketMessage,
63    ) -> Result<usize> {
64        self.gateway.broadcast_to_room(room, message).await
65    }
66}
67
68impl fmt::Debug for WebSocketGatewayServer {
69    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70        f.debug_struct("WebSocketGatewayServer")
71            .field("path", &self.path())
72            .field("namespace", &self.namespace())
73            .finish_non_exhaustive()
74    }
75}