Skip to main content

MessageObserver

Trait MessageObserver 

Source
pub trait MessageObserver: Send + Sync {
    // Provided methods
    fn handle_frame<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        frame: &'life1 Frame,
        connection_id: Option<&'life2 str>,
    ) -> Pin<Box<dyn Future<Output = Result<Option<Frame>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait { ... }
    fn handle_system_command<'life0, 'life1, 'life2, 'life3, 'async_trait>(
        &'life0 self,
        _sys_cmd: &'life1 SystemCommand,
        _frame: &'life2 Frame,
        _connection_id: Option<&'life3 str>,
    ) -> Pin<Box<dyn Future<Output = Result<Option<Frame>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait,
             'life3: 'async_trait { ... }
    fn handle_message_command<'life0, 'life1, 'life2, 'life3, 'async_trait>(
        &'life0 self,
        _msg_cmd: &'life1 PayloadCommand,
        _frame: &'life2 Frame,
        _connection_id: Option<&'life3 str>,
    ) -> Pin<Box<dyn Future<Output = Result<Option<Frame>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait,
             'life3: 'async_trait { ... }
    fn handle_notification_command<'life0, 'life1, 'life2, 'life3, 'async_trait>(
        &'life0 self,
        _notif_cmd: &'life1 NotificationCommand,
        _frame: &'life2 Frame,
        _connection_id: Option<&'life3 str>,
    ) -> Pin<Box<dyn Future<Output = Result<Option<Frame>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait,
             'life3: 'async_trait { ... }
    fn handle_custom_command<'life0, 'life1, 'life2, 'life3, 'async_trait>(
        &'life0 self,
        _custom_cmd: &'life1 CustomCommand,
        _frame: &'life2 Frame,
        _connection_id: Option<&'life3 str>,
    ) -> Pin<Box<dyn Future<Output = Result<Option<Frame>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait,
             'life3: 'async_trait { ... }
    fn handle_connection_event<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        _event: &'life1 ConnectionEvent,
        _connection_id: Option<&'life2 str>,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait { ... }
}
Expand description

消息观察者标准接口

这是消息处理的核心接口,定义了如何处理不同类型的消息和事件。 服务端和客户端都可以实现这个接口来自定义消息处理逻辑。

§设计原则

  1. 分层处理: 先处理系统命令,再处理业务命令
  2. 可扩展性: 支持自定义命令类型处理
  3. 响应式: 可以返回响应 Frame 用于请求-响应模式

Provided Methods§

Source

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

处理收到的消息 Frame

§参数
  • frame: 收到的消息 Frame
  • connection_id: 连接 ID(服务端)或 None(客户端)
§返回
  • Ok(Some(Frame)): 需要发送的响应 Frame(用于请求-响应模式)
  • Ok(None): 不需要响应
  • Err: 处理失败
§说明

这个方法会处理所有类型的命令(System、Message、Notification、Custom) 默认实现会根据命令类型分发到对应的处理方法

Source

fn handle_system_command<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, _sys_cmd: &'life1 SystemCommand, _frame: &'life2 Frame, _connection_id: Option<&'life3 str>, ) -> Pin<Box<dyn Future<Output = Result<Option<Frame>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait,

处理系统命令

默认实现返回 None,子类可以重写此方法

Source

fn handle_message_command<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, _msg_cmd: &'life1 PayloadCommand, _frame: &'life2 Frame, _connection_id: Option<&'life3 str>, ) -> Pin<Box<dyn Future<Output = Result<Option<Frame>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait,

处理消息命令

默认实现返回 None,子类可以重写此方法

Source

fn handle_notification_command<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, _notif_cmd: &'life1 NotificationCommand, _frame: &'life2 Frame, _connection_id: Option<&'life3 str>, ) -> Pin<Box<dyn Future<Output = Result<Option<Frame>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait,

处理通知命令

默认实现返回 None,子类可以重写此方法

Source

fn handle_custom_command<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, _custom_cmd: &'life1 CustomCommand, _frame: &'life2 Frame, _connection_id: Option<&'life3 str>, ) -> Pin<Box<dyn Future<Output = Result<Option<Frame>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait,

处理自定义命令(如 SyncMessages、ListSessions 等)

默认实现返回 None,子类可以重写此方法

Source

fn handle_connection_event<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, _event: &'life1 ConnectionEvent, _connection_id: Option<&'life2 str>, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

处理连接事件

当连接状态发生变化时调用

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§