From 996d2a327f175fdf2db4eacdf06608494828d9d4 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sun, 2 Jun 2024 23:52:12 +0800 Subject: [PATCH] try tailchat --- Cargo.lock | 4 ++ Cargo.toml | 3 +- ica-rs/src/tailchat.rs | 101 +++++++++++++++++++--------------- ica-rs/src/tailchat/events.rs | 20 +++++-- 4 files changed, 79 insertions(+), 49 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 81a6d05..f3d9ee8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1269,6 +1269,8 @@ dependencies = [ [[package]] name = "rust_engineio" version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3d3572ceba6c5d79eecedf3be93640ca9512fa4100dff6a70f96c514adf4f1f" dependencies = [ "adler32", "async-stream", @@ -1291,6 +1293,8 @@ dependencies = [ [[package]] name = "rust_socketio" version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a6a8672db895d567b3c0b8a4c0d3e98113ebb32badf6ce66004e743e5ee1e1e" dependencies = [ "adler32", "async-stream", diff --git a/Cargo.toml b/Cargo.toml index 982eab2..7b32dcd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,5 +5,6 @@ members = [ resolver = "2" [patch.crates-io] -# rust_socketio = { git = "https://github.com/shenjackyuanjie/rust-socketio.git", branch = "main" } +# rust_socketio = { git = "https://github.com/shenjackyuanjie/rust-socketio.git", branch = "message_pack" } +# rust_socketio = { path = "../../rust-socketio/socketio" } # pyo3 = { git = "https://github.com/PyO3/pyo3.git", branch = "main" } diff --git a/ica-rs/src/tailchat.rs b/ica-rs/src/tailchat.rs index 5ba2c27..4f85e1b 100644 --- a/ica-rs/src/tailchat.rs +++ b/ica-rs/src/tailchat.rs @@ -5,7 +5,7 @@ use futures_util::FutureExt; use md5::{Digest, Md5}; use reqwest::{Body, ClientBuilder as reqwest_ClientBuilder}; use rust_socketio::asynchronous::{Client, ClientBuilder}; -use rust_socketio::packet::PacketParser; +// use rust_socketio::packet::PacketParser; use rust_socketio::{Event, Payload, TransportType}; use serde_json::{from_str, from_value, json, Value}; use tracing::{event, span, Level}; @@ -15,47 +15,47 @@ use crate::data_struct::tailchat::status::LoginData; use crate::error::{ClientResult, TailchatError}; use crate::{wrap_any_callback, wrap_callback, StopGetter}; -mod packet_parser { - use bytes::{buf, Bytes}; - use rust_socketio::error::Error; - use rust_socketio::packet::{Packet, PacketId}; - use serde::{Deserialize, Serialize}; - use serde_json::Value as JsonValue; - use msgpack_simple::{MsgPack, MapElement} +// mod packet_parser { +// use bytes::{buf, Bytes}; +// use rust_socketio::error::Error; +// use rust_socketio::packet::{Packet, PacketId}; +// use serde::{Deserialize, Serialize}; +// use serde_json::Value as JsonValue; +// use msgpack_simple::{MsgPack, MapElement} - // #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] - // #[serde(rename = "map")] - // struct SerdeValue { - // #[serde(rename = "@type")] - // r#type: i64, - // data: rmpv::Value, - // nsp: String, - // } +// // #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] +// // #[serde(rename = "map")] +// // struct SerdeValue { +// // #[serde(rename = "@type")] +// // r#type: i64, +// // data: rmpv::Value, +// // nsp: String, +// // } - pub fn encode(packet: &Packet) -> Bytes { - let raw_str = packet.data.as_ref().map(|v| v.to_string()).unwrap_or("{}".to_string()); - let raw_value: rmpv::Value = serde_json::from_str(&raw_str).unwrap(); - let value = SerdeValue { - r#type: packet.packet_type as u8 as i64, - data: raw_value, - nsp: packet.nsp.clone(), - }; - let mut buffer = rmp_serde::to_vec(&value).unwrap(); - println!("encoding packet: {:?} {:?}", packet, value); +// pub fn encode(packet: &Packet) -> Bytes { +// let raw_str = packet.data.as_ref().map(|v| v.to_string()).unwrap_or("{}".to_string()); +// let raw_value: rmpv::Value = serde_json::from_str(&raw_str).unwrap(); +// let value = SerdeValue { +// r#type: packet.packet_type as u8 as i64, +// data: raw_value, +// nsp: packet.nsp.clone(), +// }; +// let mut buffer = rmp_serde::to_vec(&value).unwrap(); +// println!("encoding packet: {:?} {:?}", packet, value); - buffer.reverse(); - // 前面加上 \x83\xa4 - buffer.push(0xa4); - buffer.push(0x83); - buffer.reverse(); - Bytes::from(buffer) - } +// buffer.reverse(); +// // 前面加上 \x83\xa4 +// buffer.push(0xa4); +// buffer.push(0x83); +// buffer.reverse(); +// Bytes::from(buffer) +// } - pub fn default_decode(payload: &Bytes) -> Result { - println!("decoding bytes {:?}", payload); - todo!() - } -} +// pub fn default_decode(payload: &Bytes) -> Result { +// println!("decoding bytes {:?}", payload); +// todo!() +// } +// } pub async fn start_tailchat( config: TailchatConfig, @@ -101,20 +101,33 @@ pub async fn start_tailchat( Err(e) => return Err(TailchatError::LoginFailed(e.to_string())), }; - header_map.append("X-Token", status.jwt.clone().parse().unwrap()); + // header_map.append("X-Token", status.jwt.clone().parse().unwrap()); - let packet_parser = - PacketParser::new(Box::new(packet_parser::encode), Box::new(packet_parser::default_decode)); + // let packet_parser = + // PacketParser::new(Box::new(packet_parser::encode), Box::new(packet_parser::default_decode)); let socket = ClientBuilder::new(config.host) .auth(json!({"token": status.jwt.clone()})) .transport_type(TransportType::Websocket) - .packet_parser(packet_parser) .on_any(wrap_any_callback!(events::any_event)) - .on("chat.message.sendMessage", wrap_callback!(events::on_message)) + // .on("connect", wrap_callback!(events::on_connect)) + // .on("chat.message.sendMessage", wrap_callback!(events::on_message)) .connect() - .await?; + .await + .unwrap(); + event!(Level::INFO, "tailchat connected"); + + socket.emit("chat.converse.findAndJoinRoom", json!([])).await.unwrap(); + socket + .emit( + "chat.message.sendMessage", + json!({"content": "shenbot v 0.1.0", "roomId": 1, "type": 1}), + ) + .await + .unwrap(); + + event!(Level::INFO, "tailchat joined room"); // notify:chat.message.delete // notify:chat.message.add diff --git a/ica-rs/src/tailchat/events.rs b/ica-rs/src/tailchat/events.rs index 5755640..c56f850 100644 --- a/ica-rs/src/tailchat/events.rs +++ b/ica-rs/src/tailchat/events.rs @@ -1,16 +1,21 @@ use colored::Colorize; use rust_socketio::asynchronous::Client; use rust_socketio::{Event, Payload}; +use serde_json::json; use tracing::info; /// 所有 pub async fn any_event(event: Event, payload: Payload, _client: Client) { - let handled = vec![ + let handled = [ // 真正处理过的 - "chat.message.sendMessage", // 也许以后会用到 + "chat.message.sendMessage", + // "notify:chat.message.add", + // 也许以后会用到 - // 忽略的 + // 忽略的 ]; + println!("event: {:?}", event); + println!("payload: {:?}", payload); match &event { Event::Custom(event_name) => { if handled.contains(&event_name.as_str()) { @@ -27,7 +32,9 @@ pub async fn any_event(event: Event, payload: Payload, _client: Client) { info!("收到消息 {}", value.to_string().yellow()); } } - _ => (), + _ => { + return; + } } return; } @@ -57,3 +64,8 @@ pub async fn on_message(payload: Payload, client: Client) { _ => (), } } + +pub async fn on_connect(payload: Payload, client: Client) { + let _ = client.emit("chat.converse.findAndJoinRoom", json! {[]}).await; + info!("连接成功 {:?}", payload); +}