2024-03-30 18:30:43 +08:00
|
|
|
use colored::Colorize;
|
|
|
|
use rust_socketio::asynchronous::Client;
|
|
|
|
use rust_socketio::{Event, Payload};
|
2024-06-02 23:52:12 +08:00
|
|
|
use serde_json::json;
|
2024-03-30 18:30:43 +08:00
|
|
|
use tracing::info;
|
2024-03-30 14:24:19 +08:00
|
|
|
|
2024-03-30 18:30:43 +08:00
|
|
|
/// 所有
|
|
|
|
pub async fn any_event(event: Event, payload: Payload, _client: Client) {
|
2024-06-02 23:52:12 +08:00
|
|
|
let handled = [
|
2024-03-30 18:30:43 +08:00
|
|
|
// 真正处理过的
|
2024-06-02 23:52:12 +08:00
|
|
|
"chat.message.sendMessage",
|
|
|
|
// "notify:chat.message.add",
|
|
|
|
// 也许以后会用到
|
2024-03-30 18:30:43 +08:00
|
|
|
|
2024-06-02 23:52:12 +08:00
|
|
|
// 忽略的
|
2024-03-30 18:30:43 +08:00
|
|
|
];
|
2024-06-02 23:52:12 +08:00
|
|
|
println!("event: {:?}", event);
|
|
|
|
println!("payload: {:?}", payload);
|
2024-03-30 18:30:43 +08:00
|
|
|
match &event {
|
|
|
|
Event::Custom(event_name) => {
|
|
|
|
if handled.contains(&event_name.as_str()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Event::Message => {
|
|
|
|
match payload {
|
|
|
|
Payload::Text(values) => {
|
|
|
|
if let Some(value) = values.first() {
|
|
|
|
if handled.contains(&value.as_str().unwrap()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
info!("收到消息 {}", value.to_string().yellow());
|
|
|
|
}
|
|
|
|
}
|
2024-06-02 23:52:12 +08:00
|
|
|
_ => {
|
|
|
|
return;
|
|
|
|
}
|
2024-03-30 18:30:43 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
match payload {
|
|
|
|
Payload::Binary(ref data) => {
|
|
|
|
println!("event: {} |{:?}", event, data)
|
|
|
|
}
|
|
|
|
Payload::Text(ref data) => {
|
|
|
|
print!("event: {}", event.as_str().purple());
|
|
|
|
for value in data {
|
|
|
|
println!("|{}", value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn on_message(payload: Payload, client: Client) {
|
|
|
|
match payload {
|
|
|
|
Payload::Text(values) => {
|
|
|
|
if let Some(value) = values.first() {
|
|
|
|
info!("收到消息 {}", value.to_string().yellow());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
2024-06-02 23:52:12 +08:00
|
|
|
|
|
|
|
pub async fn on_connect(payload: Payload, client: Client) {
|
|
|
|
let _ = client.emit("chat.converse.findAndJoinRoom", json! {[]}).await;
|
|
|
|
info!("连接成功 {:?}", payload);
|
|
|
|
}
|