Skip to main content

flare_core/client/events/
handler.rs

1//! 客户端事件处理器
2//!
3//! 定义客户端事件处理接口,允许用户实现自定义业务逻辑
4
5use crate::common::error::Result;
6use crate::common::protocol::Frame;
7use crate::common::protocol::flare::core::commands::notification_command::Type as NotifType;
8use crate::common::protocol::flare::core::commands::payload_command::Type as PayloadType;
9use crate::common::protocol::flare::core::commands::system_command::Type as SysType;
10use crate::transport::events::ConnectionEvent;
11
12/// 客户端事件处理器
13///
14/// 允许用户实现自定义逻辑来处理不同类型的客户端事件和命令
15///
16/// 所有方法都有默认实现,用户只需要实现需要的方法即可
17#[async_trait::async_trait]
18pub trait ClientEventHandler: Send + Sync {
19    /// 处理系统命令
20    ///
21    /// # 参数
22    /// - `command_type`: 系统命令类型(CONNECT, CONNECT_ACK, PING, PONG, KICKED, ERROR, CLOSE)
23    /// - `frame`: 完整的消息帧
24    ///
25    /// # 返回
26    /// - `Ok(Some(Frame))`: 需要发送的回复消息(可选)
27    /// - `Ok(None)`: 不需要回复
28    /// - `Err`: 处理失败
29    async fn handle_system_command(
30        &self,
31        command_type: SysType,
32        frame: &Frame,
33    ) -> Result<Option<Frame>> {
34        let _ = (command_type, frame);
35        Ok(None)
36    }
37
38    /// 处理载荷命令(MESSAGE/EVENT/ACK/DATA)
39    async fn handle_message_command(
40        &self,
41        command_type: PayloadType,
42        frame: &Frame,
43    ) -> Result<Option<Frame>> {
44        let _ = (command_type, frame);
45        Ok(None)
46    }
47
48    /// 处理通知命令
49    ///
50    /// # 参数
51    /// - `command_type`: 通知命令类型
52    /// - `frame`: 完整的消息帧
53    ///
54    /// # 返回
55    /// - `Ok(Some(Frame))`: 需要发送的回复消息(可选)
56    /// - `Ok(None)`: 不需要回复
57    /// - `Err`: 处理失败
58    async fn handle_notification_command(
59        &self,
60        command_type: NotifType,
61        frame: &Frame,
62    ) -> Result<Option<Frame>> {
63        let _ = (command_type, frame);
64        Ok(None)
65    }
66
67    /// 处理连接事件
68    ///
69    /// # 参数
70    /// - `event`: 连接事件(Connected, Disconnected, Error)
71    ///
72    /// # 返回
73    /// - `Ok(())`: 处理成功
74    /// - `Err`: 处理失败
75    async fn handle_connection_event(&self, event: &ConnectionEvent) -> Result<()> {
76        let _ = event;
77        Ok(())
78    }
79}