Skip to main content

ServerHandle

Trait ServerHandle 

Source
pub trait ServerHandle: Send + Sync {
    // Required methods
    fn send_to<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        connection_id: &'life1 str,
        frame: &'life2 Frame,
    ) -> 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,
        frame: &'life2 Frame,
    ) -> 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,
        frame: &'life1 Frame,
    ) -> 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,
        frame: &'life1 Frame,
        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;
    fn disconnect<'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 connection_count(&self) -> usize;
    fn user_count(&self) -> usize;

    // Provided method
    fn send_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 { ... }
}
Expand description

服务器操作处理器

提供消息发送和连接管理的轻量级接口 可以在任何需要发送消息或管理连接的地方注入此 trait,而不需要注入整个 Server

§示例

use flare_core::server::ServerHandle;
use flare_core::common::error::Result;
use flare_core::common::protocol::Frame;
use std::sync::Arc;

struct MyHandler {
    server_handle: Arc<dyn ServerHandle>,
}

impl MyHandler {
    async fn send_message(&self, connection_id: &str, frame: &Frame) -> Result<()> {
        self.server_handle.send_to(connection_id, frame).await
    }
}

Required Methods§

Source

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

向指定连接发送消息

§参数
  • connection_id: 目标连接 ID
  • frame: 要发送的 Frame
§返回

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

Source

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

向指定用户的所有连接发送消息

§参数
  • user_id: 目标用户 ID
  • frame: 要发送的 Frame
§返回

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

Source

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

广播消息到所有连接

§参数
  • frame: 要广播的 Frame
§返回

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

Source

fn broadcast_except<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, frame: &'life1 Frame, 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,

广播消息到所有连接,排除指定的连接

§参数
  • frame: 要广播的 Frame
  • exclude_connection_id: 要排除的连接 ID
§返回

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

Source

fn disconnect<'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,

断开指定连接

§参数
  • connection_id: 要断开的连接 ID
§返回

断开成功返回 Ok(()),失败返回错误

Source

fn connection_count(&self) -> usize

获取连接数量

§返回

当前连接数量

Source

fn user_count(&self) -> usize

获取用户数量

§返回

当前用户数量

Provided Methods§

Source

fn send_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 发送到多个连接(群扇出主路径)。 默认逐连接;带连接管理器的实现按(序列化格式, 压缩)分组、 每组序列化一次共享给组内无加密连接。返回(成功数, 失败数)。

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§

Source§

impl ServerHandle for DefaultServerHandle

Source§

impl ServerHandle for HybridServer

让 HybridServer 实现 ServerHandle trait 这样可以在任何需要发送消息的地方注入 HybridServer 的 ServerCore,而不需要整个 Server

Source§

impl ServerHandle for ServerCore

让 ServerCore 实现 ServerHandle trait 这样可以在任何需要发送消息的地方注入 ServerCore,而不需要整个 Server