完善一些

This commit is contained in:
shenjack 2024-02-18 23:17:43 +08:00
parent 4bda64d8fa
commit 06ee237bf5
Signed by: shenjack
GPG Key ID: 7B1134A979775551
3 changed files with 34 additions and 18 deletions

View File

@ -1,26 +1,29 @@
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use serde_json::Value as JsonValue; 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} /// {"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)] #[derive(Debug, Clone)]
pub struct NewMessage { pub struct NewMessage {
/// 发送者 id
pub sender_id: i64,
/// 房间 id
pub roomd_id: i64,
/// 发送者名字
pub sender_name: String,
/// 消息 id /// 消息 id
pub msg_id: String, 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 time: NaiveDateTime,
/// 身份 /// 身份
pub role: String, pub role: String,
/// 消息内容
pub content: String,
/// "群主授予的头衔" /// "群主授予的头衔"
pub title: String, pub title: String,
/// 消息内容
pub content: String,
/// 气泡 id
pub bubble_id: i64,
/// 原始消息 /// 原始消息
pub raw: JsonValue, pub raw: JsonValue,
} }
@ -30,6 +33,7 @@ impl NewMessage {
let message = json["message"].as_object()?; let message = json["message"].as_object()?;
let room_id = json["roomId"].as_i64()?; let room_id = json["roomId"].as_i64()?;
let sender_id = message.get("senderId")?.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 sender_name = message.get("username")?.as_str()?.to_string();
let msg_id = message.get("_id")?.as_str()?.to_string(); let msg_id = message.get("_id")?.as_str()?.to_string();
let time = message.get("time")?.as_i64()?; let time = message.get("time")?.as_i64()?;
@ -37,15 +41,18 @@ impl NewMessage {
let role = message.get("role")?.as_str()?.to_string(); let role = message.get("role")?.as_str()?.to_string();
let content = message.get("content")?.as_str()?.to_string(); let content = message.get("content")?.as_str()?.to_string();
let title = message.get("title")?.as_str()?.to_string(); let title = message.get("title")?.as_str()?.to_string();
let bubble_id = message.get("bubble_id")?.as_i64()?;
Some(Self { Some(Self {
sender_id,
roomd_id: room_id,
sender_name,
msg_id, msg_id,
sender_id,
subid,
room_id,
sender_name,
time, time,
role, role,
content,
title, title,
content,
bubble_id,
raw: json.clone(), 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 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(); let new_message = NewMessage::new_from_json(&value).unwrap();
assert_eq!(new_message.sender_id, 123456); 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.sender_name, "shenjack");
assert_eq!(new_message.msg_id, "idddddd"); assert_eq!(new_message.msg_id, "idddddd");
assert_eq!(new_message.role, "admin"); assert_eq!(new_message.role, "admin");

View File

@ -2,6 +2,7 @@ use colored::Colorize;
use rust_socketio::{Event, Payload, RawClient}; use rust_socketio::{Event, Payload, RawClient};
use tracing::{info, warn}; use tracing::{info, warn};
use crate::data_struct::new_message::NewMessage;
use crate::data_struct::online_data::OnlineData; use crate::data_struct::online_data::OnlineData;
use crate::py; use crate::py;
@ -20,7 +21,8 @@ pub fn get_online_data(payload: Payload, _client: RawClient) {
pub fn add_message(payload: Payload, _client: RawClient) { pub fn add_message(payload: Payload, _client: RawClient) {
if let Payload::Text(values) = payload { if let Payload::Text(values) = payload {
if let Some(value) = values.first() { if let Some(value) = values.first() {
info!("add_message {}", value); let message = NewMessage::new_from_json(value);
info!("add_message {}", format!("{:#?}", message).cyan());
} }
} }
} }

View File

@ -1,5 +1,6 @@
use std::time::Duration; use std::time::Duration;
use tracing::info;
use rust_socketio::ClientBuilder; use rust_socketio::ClientBuilder;
mod client; mod client;
@ -26,9 +27,15 @@ fn ws_main() {
.connect() .connect()
.expect("Connection failed"); .expect("Connection failed");
std::thread::sleep(Duration::from_secs(10)); 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");
socket.disconnect().expect("Disconnect failed")
} }
fn main() { fn main() {