try tailchat
This commit is contained in:
parent
4669b6a378
commit
996d2a327f
4
Cargo.lock
generated
4
Cargo.lock
generated
@ -1269,6 +1269,8 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "rust_engineio"
|
name = "rust_engineio"
|
||||||
version = "0.6.0"
|
version = "0.6.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d3d3572ceba6c5d79eecedf3be93640ca9512fa4100dff6a70f96c514adf4f1f"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"adler32",
|
"adler32",
|
||||||
"async-stream",
|
"async-stream",
|
||||||
@ -1291,6 +1293,8 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "rust_socketio"
|
name = "rust_socketio"
|
||||||
version = "0.6.0"
|
version = "0.6.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6a6a8672db895d567b3c0b8a4c0d3e98113ebb32badf6ce66004e743e5ee1e1e"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"adler32",
|
"adler32",
|
||||||
"async-stream",
|
"async-stream",
|
||||||
|
@ -5,5 +5,6 @@ members = [
|
|||||||
resolver = "2"
|
resolver = "2"
|
||||||
|
|
||||||
[patch.crates-io]
|
[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" }
|
# pyo3 = { git = "https://github.com/PyO3/pyo3.git", branch = "main" }
|
||||||
|
@ -5,7 +5,7 @@ use futures_util::FutureExt;
|
|||||||
use md5::{Digest, Md5};
|
use md5::{Digest, Md5};
|
||||||
use reqwest::{Body, ClientBuilder as reqwest_ClientBuilder};
|
use reqwest::{Body, ClientBuilder as reqwest_ClientBuilder};
|
||||||
use rust_socketio::asynchronous::{Client, ClientBuilder};
|
use rust_socketio::asynchronous::{Client, ClientBuilder};
|
||||||
use rust_socketio::packet::PacketParser;
|
// use rust_socketio::packet::PacketParser;
|
||||||
use rust_socketio::{Event, Payload, TransportType};
|
use rust_socketio::{Event, Payload, TransportType};
|
||||||
use serde_json::{from_str, from_value, json, Value};
|
use serde_json::{from_str, from_value, json, Value};
|
||||||
use tracing::{event, span, Level};
|
use tracing::{event, span, Level};
|
||||||
@ -15,47 +15,47 @@ use crate::data_struct::tailchat::status::LoginData;
|
|||||||
use crate::error::{ClientResult, TailchatError};
|
use crate::error::{ClientResult, TailchatError};
|
||||||
use crate::{wrap_any_callback, wrap_callback, StopGetter};
|
use crate::{wrap_any_callback, wrap_callback, StopGetter};
|
||||||
|
|
||||||
mod packet_parser {
|
// mod packet_parser {
|
||||||
use bytes::{buf, Bytes};
|
// use bytes::{buf, Bytes};
|
||||||
use rust_socketio::error::Error;
|
// use rust_socketio::error::Error;
|
||||||
use rust_socketio::packet::{Packet, PacketId};
|
// use rust_socketio::packet::{Packet, PacketId};
|
||||||
use serde::{Deserialize, Serialize};
|
// use serde::{Deserialize, Serialize};
|
||||||
use serde_json::Value as JsonValue;
|
// use serde_json::Value as JsonValue;
|
||||||
use msgpack_simple::{MsgPack, MapElement}
|
// use msgpack_simple::{MsgPack, MapElement}
|
||||||
|
|
||||||
// #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
|
// // #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
|
||||||
// #[serde(rename = "map")]
|
// // #[serde(rename = "map")]
|
||||||
// struct SerdeValue {
|
// // struct SerdeValue {
|
||||||
// #[serde(rename = "@type")]
|
// // #[serde(rename = "@type")]
|
||||||
// r#type: i64,
|
// // r#type: i64,
|
||||||
// data: rmpv::Value,
|
// // data: rmpv::Value,
|
||||||
// nsp: String,
|
// // 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);
|
||||||
|
|
||||||
|
// buffer.reverse();
|
||||||
|
// // 前面加上 \x83\xa4
|
||||||
|
// buffer.push(0xa4);
|
||||||
|
// buffer.push(0x83);
|
||||||
|
// buffer.reverse();
|
||||||
|
// Bytes::from(buffer)
|
||||||
// }
|
// }
|
||||||
|
|
||||||
pub fn encode(packet: &Packet) -> Bytes {
|
// pub fn default_decode(payload: &Bytes) -> Result<Packet, Error> {
|
||||||
let raw_str = packet.data.as_ref().map(|v| v.to_string()).unwrap_or("{}".to_string());
|
// println!("decoding bytes {:?}", payload);
|
||||||
let raw_value: rmpv::Value = serde_json::from_str(&raw_str).unwrap();
|
// todo!()
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn default_decode(payload: &Bytes) -> Result<Packet, Error> {
|
|
||||||
println!("decoding bytes {:?}", payload);
|
|
||||||
todo!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn start_tailchat(
|
pub async fn start_tailchat(
|
||||||
config: TailchatConfig,
|
config: TailchatConfig,
|
||||||
@ -101,20 +101,33 @@ pub async fn start_tailchat(
|
|||||||
Err(e) => return Err(TailchatError::LoginFailed(e.to_string())),
|
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 =
|
// let packet_parser =
|
||||||
PacketParser::new(Box::new(packet_parser::encode), Box::new(packet_parser::default_decode));
|
// PacketParser::new(Box::new(packet_parser::encode), Box::new(packet_parser::default_decode));
|
||||||
|
|
||||||
let socket = ClientBuilder::new(config.host)
|
let socket = ClientBuilder::new(config.host)
|
||||||
.auth(json!({"token": status.jwt.clone()}))
|
.auth(json!({"token": status.jwt.clone()}))
|
||||||
.transport_type(TransportType::Websocket)
|
.transport_type(TransportType::Websocket)
|
||||||
.packet_parser(packet_parser)
|
|
||||||
.on_any(wrap_any_callback!(events::any_event))
|
.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()
|
.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.delete
|
||||||
// notify:chat.message.add
|
// notify:chat.message.add
|
||||||
|
|
||||||
|
@ -1,16 +1,21 @@
|
|||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
use rust_socketio::asynchronous::Client;
|
use rust_socketio::asynchronous::Client;
|
||||||
use rust_socketio::{Event, Payload};
|
use rust_socketio::{Event, Payload};
|
||||||
|
use serde_json::json;
|
||||||
use tracing::info;
|
use tracing::info;
|
||||||
|
|
||||||
/// 所有
|
/// 所有
|
||||||
pub async fn any_event(event: Event, payload: Payload, _client: Client) {
|
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 {
|
match &event {
|
||||||
Event::Custom(event_name) => {
|
Event::Custom(event_name) => {
|
||||||
if handled.contains(&event_name.as_str()) {
|
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());
|
info!("收到消息 {}", value.to_string().yellow());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => (),
|
_ => {
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
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);
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user