From 06ee237bf51c5120b34dbd826d89a2ebadface6d Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sun, 18 Feb 2024 23:17:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E4=B8=80=E4=BA=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ica-rs/src/data_struct/new_message.rs | 35 ++++++++++++++++----------- ica-rs/src/events.rs | 4 ++- ica-rs/src/main.rs | 13 +++++++--- 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/ica-rs/src/data_struct/new_message.rs b/ica-rs/src/data_struct/new_message.rs index 934b8b1..13756c5 100644 --- a/ica-rs/src/data_struct/new_message.rs +++ b/ica-rs/src/data_struct/new_message.rs @@ -1,26 +1,29 @@ use chrono::NaiveDateTime; use serde_json::Value as JsonValue; -use tracing::warn; /// {"message": {"_id":"idddddd","anonymousId":null,"anonymousflag":null,"bubble_id":0,"content":"test","date":"2024/02/18","files":[],"role":"admin","senderId":123456,"subid":1,"time":1708267062000_i64,"timestamp":"22:37:42","title":"索引管理员","username":"shenjack"},"roomId":-123456} #[derive(Debug, Clone)] pub struct NewMessage { - /// 发送者 id - pub sender_id: i64, - /// 房间 id - pub roomd_id: i64, - /// 发送者名字 - pub sender_name: String, /// 消息 id pub msg_id: String, + /// 发送者 id + pub sender_id: i64, + /// 子? id + pub subid: i64, + /// 房间 id + pub room_id: i64, + /// 发送者名字 + pub sender_name: String, /// 消息时间 pub time: NaiveDateTime, /// 身份 pub role: String, - /// 消息内容 - pub content: String, /// "群主授予的头衔" pub title: String, + /// 消息内容 + pub content: String, + /// 气泡 id + pub bubble_id: i64, /// 原始消息 pub raw: JsonValue, } @@ -30,6 +33,7 @@ impl NewMessage { let message = json["message"].as_object()?; let room_id = json["roomId"].as_i64()?; let sender_id = message.get("senderId")?.as_i64()?; + let subid = message.get("subid")?.as_i64()?; let sender_name = message.get("username")?.as_str()?.to_string(); let msg_id = message.get("_id")?.as_str()?.to_string(); let time = message.get("time")?.as_i64()?; @@ -37,15 +41,18 @@ impl NewMessage { let role = message.get("role")?.as_str()?.to_string(); let content = message.get("content")?.as_str()?.to_string(); let title = message.get("title")?.as_str()?.to_string(); + let bubble_id = message.get("bubble_id")?.as_i64()?; Some(Self { - sender_id, - roomd_id: room_id, - sender_name, msg_id, + sender_id, + subid, + room_id, + sender_name, time, role, - content, title, + content, + bubble_id, raw: json.clone(), }) } @@ -62,7 +69,7 @@ mod test { let value = json!({"message": {"_id":"idddddd","anonymousId":null,"anonymousflag":null,"bubble_id":0,"content":"test","date":"2024/02/18","files":[],"role":"admin","senderId":123456,"subid":1,"time":1708267062000_i64,"timestamp":"22:37:42","title":"索引管理员","username":"shenjack"},"roomId":-123456}); let new_message = NewMessage::new_from_json(&value).unwrap(); assert_eq!(new_message.sender_id, 123456); - assert_eq!(new_message.roomd_id, -123456); + assert_eq!(new_message.room_id, -123456); assert_eq!(new_message.sender_name, "shenjack"); assert_eq!(new_message.msg_id, "idddddd"); assert_eq!(new_message.role, "admin"); diff --git a/ica-rs/src/events.rs b/ica-rs/src/events.rs index 10a15e5..67d9f17 100644 --- a/ica-rs/src/events.rs +++ b/ica-rs/src/events.rs @@ -2,6 +2,7 @@ use colored::Colorize; use rust_socketio::{Event, Payload, RawClient}; use tracing::{info, warn}; +use crate::data_struct::new_message::NewMessage; use crate::data_struct::online_data::OnlineData; use crate::py; @@ -20,7 +21,8 @@ pub fn get_online_data(payload: Payload, _client: RawClient) { pub fn add_message(payload: Payload, _client: RawClient) { if let Payload::Text(values) = payload { if let Some(value) = values.first() { - info!("add_message {}", value); + let message = NewMessage::new_from_json(value); + info!("add_message {}", format!("{:#?}", message).cyan()); } } } diff --git a/ica-rs/src/main.rs b/ica-rs/src/main.rs index 6d4ea6b..d22acbe 100644 --- a/ica-rs/src/main.rs +++ b/ica-rs/src/main.rs @@ -1,5 +1,6 @@ use std::time::Duration; +use tracing::info; use rust_socketio::ClientBuilder; mod client; @@ -26,9 +27,15 @@ fn ws_main() { .connect() .expect("Connection failed"); - std::thread::sleep(Duration::from_secs(10)); - - socket.disconnect().expect("Disconnect failed") + info!("Connected"); + std::thread::sleep(Duration::from_secs(3)); + // 等待一个输入 + info!("Press any key to exit"); + let mut input = String::new(); + std::io::stdin().read_line(&mut input).unwrap(); + socket.disconnect().expect("Disconnect failed"); + info!("Disconnected"); + } fn main() {