flare_im_core/client/
message_handler.rs

1use async_trait::async_trait;
2use log::debug;
3use flare_core::flare_net::net::{Command, Response};
4
5#[async_trait]
6pub trait MessageHandler: Send + Sync + 'static {
7    /// 处理消息
8    async fn on_message(&self, msg: Vec<u8>);
9
10    /// 处理自定义消息
11    async fn on_custom_message(&self, msg: Vec<u8>);
12
13    /// 处理通知消息
14    async fn on_notice_message(&self, msg: Vec<u8>);
15
16    /// 处理响应
17    async fn on_response(&self, msg: &Response);
18    /// 处理服务端的ack
19    async fn on_ack_message(&self, msg: Vec<u8>);
20    /// 处理数据消息
21    async fn on_data(&self, data: Vec<u8>);
22    /// 获取支持的命令列表
23    fn supported_commands(&self) -> Vec<Command>{
24        vec![ Command::ServerPushMsg , Command::ServerPushCustom ,
25            Command::ServerPushNotice , Command::ServerPushData,Command::ServerResponse,Command::ServerAck]
26    }
27
28    /// 检查是否支持某个命令
29    fn supports_command(&self, command: Command) -> bool {
30        self.supported_commands().contains(&command)
31    }
32}
33
34#[derive(Default)]
35pub struct DefMessageHandler;
36
37impl DefMessageHandler {
38    pub fn new() -> Self {
39        Self
40    }
41}
42
43#[async_trait]
44impl MessageHandler for DefMessageHandler {
45    async fn on_message(&self, msg: Vec<u8>){
46        debug!("收到消息: {} bytes", msg.len());
47        
48    }
49
50    async fn on_custom_message(&self, msg: Vec<u8>) {
51        debug!("收到自定义消息: {} bytes", msg.len());
52        
53    }
54
55    async fn on_notice_message(&self, msg: Vec<u8>) {
56        debug!("收到通知消息: {} bytes", msg.len());
57        
58    }
59
60    async fn on_response(&self, msg: &Response) {
61        debug!("收到响应: {:?}", msg);
62    }
63
64    async fn on_ack_message(&self, msg: Vec<u8>) {
65        debug!("收到ack消息: {:?}", msg);
66    }
67
68    async fn on_data(&self, data: Vec<u8>){
69        debug!("收到数据: {} bytes", data.len());
70        
71    }
72}