Skip to main content

ConnectionManagerTrait

Trait ConnectionManagerTrait 

Source
pub trait ConnectionManagerTrait:
    Send
    + Sync
    + Any {
Show 22 methods // Required methods fn as_any(&self) -> &dyn Any; fn add_connection<'life0, 'async_trait>( &'life0 self, connection_id: String, connection: Arc<Mutex<Box<dyn Connection>>>, user_id: Option<String>, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait; fn remove_connection<'life0, 'life1, 'async_trait>( &'life0 self, connection_id: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait; fn get_connection<'life0, 'life1, 'async_trait>( &'life0 self, connection_id: &'life1 str, ) -> Pin<Box<dyn Future<Output = Option<(Arc<Mutex<Box<dyn Connection>>>, ConnectionInfo)>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait; fn get_user_connections<'life0, 'life1, 'async_trait>( &'life0 self, user_id: &'life1 str, ) -> Pin<Box<dyn Future<Output = Vec<String>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait; fn bind_user<'life0, 'life1, 'async_trait>( &'life0 self, connection_id: &'life1 str, user_id: String, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait; fn update_connection_active<'life0, 'life1, 'async_trait>( &'life0 self, connection_id: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait; fn set_connection_authenticated<'life0, 'life1, 'async_trait>( &'life0 self, connection_id: &'life1 str, user_id: Option<String>, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait; fn list_connections<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Vec<String>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait; fn connection_count<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = usize> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait; fn connection_count_snapshot(&self) -> usize; fn user_count_snapshot(&self) -> usize; fn cleanup_timeout_connections<'life0, 'async_trait>( &'life0 self, timeout: Duration, ) -> Pin<Box<dyn Future<Output = Vec<String>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait; fn send_to_connection<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, connection_id: &'life1 str, data: &'life2 [u8], ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait; fn send_to_user<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, user_id: &'life1 str, data: &'life2 [u8], ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait; fn broadcast<'life0, 'life1, 'async_trait>( &'life0 self, data: &'life1 [u8], ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait; fn broadcast_except<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, data: &'life1 [u8], exclude_connection_id: &'life2 str, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait; // Provided methods fn send_frame_to<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, connection_id: &'life1 str, frame: &'life2 Frame, parser: Option<&'life3 MessageParser>, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait { ... } fn send_frame_to_user<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, user_id: &'life1 str, frame: &'life2 Frame, parser: Option<&'life3 MessageParser>, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait { ... } fn send_frame_to_connections<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, connection_ids: &'life1 [String], frame: &'life2 Frame, ) -> Pin<Box<dyn Future<Output = (i32, i32)> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait { ... } fn broadcast_frame<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, frame: &'life1 Frame, parser: Option<&'life2 MessageParser>, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait { ... } fn broadcast_frame_except<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, frame: &'life1 Frame, exclude_connection_id: &'life2 str, parser: Option<&'life3 MessageParser>, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait { ... }
}
Expand description

连接管理器抽象 trait

实现此 trait 以提供自定义的连接管理逻辑 例如:基于 Redis 的分布式连接管理、基于数据库的持久化等

Required Methods§

Source

fn as_any(&self) -> &dyn Any

获取 Any 引用,用于类型向下转换

Source

fn add_connection<'life0, 'async_trait>( &'life0 self, connection_id: String, connection: Arc<Mutex<Box<dyn Connection>>>, user_id: Option<String>, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

添加连接

Source

fn remove_connection<'life0, 'life1, 'async_trait>( &'life0 self, connection_id: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

移除连接

Source

fn get_connection<'life0, 'life1, 'async_trait>( &'life0 self, connection_id: &'life1 str, ) -> Pin<Box<dyn Future<Output = Option<(Arc<Mutex<Box<dyn Connection>>>, ConnectionInfo)>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

获取连接

Source

fn get_user_connections<'life0, 'life1, 'async_trait>( &'life0 self, user_id: &'life1 str, ) -> Pin<Box<dyn Future<Output = Vec<String>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

获取用户的所有连接 ID

Source

fn bind_user<'life0, 'life1, 'async_trait>( &'life0 self, connection_id: &'life1 str, user_id: String, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

绑定用户到连接

Source

fn update_connection_active<'life0, 'life1, 'async_trait>( &'life0 self, connection_id: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

更新连接的最后活跃时间

Source

fn set_connection_authenticated<'life0, 'life1, 'async_trait>( &'life0 self, connection_id: &'life1 str, user_id: Option<String>, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

设置连接为已验证状态(认证通过后调用)

Source

fn list_connections<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Vec<String>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

获取所有连接 ID

Source

fn connection_count<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = usize> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

获取连接总数

Source

fn connection_count_snapshot(&self) -> usize

Returns a synchronous snapshot of the total connection count.

Implementations must use cheap local state such as cached counters or atomics. Distributed implementations may return their most recent local snapshot.

Source

fn user_count_snapshot(&self) -> usize

Returns a synchronous user-count snapshot with the same constraints as Self::connection_count_snapshot.

Source

fn cleanup_timeout_connections<'life0, 'async_trait>( &'life0 self, timeout: Duration, ) -> Pin<Box<dyn Future<Output = Vec<String>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

清理超时连接

Source

fn send_to_connection<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, connection_id: &'life1 str, data: &'life2 [u8], ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

向指定连接发送数据(字节数组)

Source

fn send_to_user<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, user_id: &'life1 str, data: &'life2 [u8], ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

向指定用户的所有连接发送数据(字节数组)

Source

fn broadcast<'life0, 'life1, 'async_trait>( &'life0 self, data: &'life1 [u8], ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

广播消息到所有连接(字节数组)

Source

fn broadcast_except<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, data: &'life1 [u8], exclude_connection_id: &'life2 str, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

广播消息到所有连接,排除指定连接(字节数组)

Provided Methods§

Source

fn send_frame_to<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, connection_id: &'life1 str, frame: &'life2 Frame, parser: Option<&'life3 MessageParser>, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait,

向指定连接发送 Frame(自动序列化)

§参数
  • connection_id: 连接 ID
  • frame: 要发送的 Frame
  • parser: 消息解析器,用于序列化 Frame(如果为 None,则从连接的协商信息创建)
§返回

发送成功返回 Ok(()),失败返回错误

§注意

如果 parser 为 None,将从连接的 ConnectionInfo 中获取协商后的序列化格式和压缩算法创建 parser

Source

fn send_frame_to_user<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, user_id: &'life1 str, frame: &'life2 Frame, parser: Option<&'life3 MessageParser>, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait,

向指定用户的所有连接发送 Frame(自动序列化)

§参数
  • user_id: 用户 ID
  • frame: 要发送的 Frame
  • parser: 消息解析器,用于序列化 Frame(如果为 None,则为每个连接使用其协商的格式)
§返回

发送成功返回 Ok(()),失败返回错误

Source

fn send_frame_to_connections<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, connection_ids: &'life1 [String], frame: &'life2 Frame, ) -> Pin<Box<dyn Future<Output = (i32, i32)> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

同一 Frame 发送到多个连接(群扇出主路径)。

默认逐连接(每连接自协商格式序列化);连接管理器实现按 (序列化格式, 压缩)分组、每组序列化一次共享给组内无加密连接。

§返回

(成功连接数, 失败连接数)

Source

fn broadcast_frame<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, frame: &'life1 Frame, parser: Option<&'life2 MessageParser>, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

广播 Frame 到所有连接(自动序列化)

§参数
  • frame: 要广播的 Frame
  • parser: 消息解析器,用于序列化 Frame(如果为 None,则为每个连接使用其协商的格式)
§返回

广播成功返回 Ok(()),失败返回错误

Source

fn broadcast_frame_except<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, frame: &'life1 Frame, exclude_connection_id: &'life2 str, parser: Option<&'life3 MessageParser>, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait,

广播 Frame 到所有连接,排除指定连接(自动序列化)

§参数
  • frame: 要广播的 Frame
  • exclude_connection_id: 要排除的连接 ID
  • parser: 消息解析器,用于序列化 Frame(如果为 None,则为每个连接使用其协商的格式)
§返回

广播成功返回 Ok(()),失败返回错误

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§