2023-12-11 01:17:25 +08:00
|
|
|
mod client;
|
|
|
|
|
2023-12-11 00:00:01 +08:00
|
|
|
use ed25519_dalek::{Signature, Signer, SigningKey};
|
2023-12-11 18:17:55 +08:00
|
|
|
use rust_socketio::{ClientBuilder, Event, Payload, RawClient};
|
2023-12-11 21:27:08 +08:00
|
|
|
use serde_json::Value;
|
2023-12-11 00:00:01 +08:00
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
#[allow(unused)]
|
2023-12-11 21:27:08 +08:00
|
|
|
fn any_event(event: Event, payload: Payload, _client: RawClient, id: Option<i32>) {
|
2023-12-24 17:27:15 +08:00
|
|
|
match payload {
|
|
|
|
Payload::Binary(ref data) => {
|
|
|
|
println!("event: {} |{:?}|id{:?}", event, data, id)
|
|
|
|
}
|
|
|
|
Payload::Text(ref data) => {
|
|
|
|
print!("\x1b[35mevent: {event}\x1b[0m");
|
|
|
|
for value in data {
|
|
|
|
print!("|{}", value.to_string());
|
|
|
|
}
|
|
|
|
println!("|id:{:?}|", id);
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
// println!("event: {} |{:?}|id{:?}", event, payload, id)
|
2023-12-11 00:00:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn ws_main() {
|
|
|
|
// define a callback which is called when a payload is received
|
|
|
|
// this callback gets the payload as well as an instance of the
|
|
|
|
// socket to communicate with the server
|
2023-12-24 17:27:15 +08:00
|
|
|
let connect_call_back = |payload: Payload, _client: RawClient, _id| match payload {
|
|
|
|
Payload::Text(values) => {
|
|
|
|
if let Some(value) = values.first() {
|
|
|
|
if let Some("authSucceed") = value.as_str() {
|
|
|
|
println!("\x1b[32m已经登录到 icalingua!\x1b[0m");
|
2023-12-12 01:42:09 +08:00
|
|
|
}
|
2023-12-24 17:27:15 +08:00
|
|
|
}
|
2023-12-12 01:42:09 +08:00
|
|
|
}
|
2023-12-24 17:27:15 +08:00
|
|
|
_ => (),
|
2023-12-11 00:00:01 +08:00
|
|
|
};
|
2023-12-11 18:17:55 +08:00
|
|
|
// 从命令行获取 host 和 key
|
2023-12-24 17:27:15 +08:00
|
|
|
// 从命令行获取配置文件路径
|
|
|
|
let config_path = std::env::args().nth(1).expect("No config path given");
|
|
|
|
let ica_singer = client::IcalinguaSinger::new_from_config(config_path);
|
2023-12-11 00:00:01 +08:00
|
|
|
|
|
|
|
// get a socket that is connected to the admin namespace
|
|
|
|
|
2023-12-24 17:27:15 +08:00
|
|
|
let socket = ClientBuilder::new(ica_singer.host.clone())
|
2023-12-11 00:00:01 +08:00
|
|
|
// .namespace("/admin")
|
|
|
|
.on_any(any_event)
|
2023-12-12 01:42:09 +08:00
|
|
|
.on("message", connect_call_back)
|
2023-12-24 17:27:15 +08:00
|
|
|
.on("requireAuth", move |a, b, c| ica_singer.sign_callback(a, b, c))
|
2023-12-11 00:00:01 +08:00
|
|
|
.connect()
|
|
|
|
.expect("Connection failed");
|
|
|
|
|
|
|
|
std::thread::sleep(Duration::from_secs(10));
|
|
|
|
|
|
|
|
socket.disconnect().expect("Disconnect failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
ws_main();
|
|
|
|
}
|