Skip to content

Commit

Permalink
fix kook
Browse files Browse the repository at this point in the history
  • Loading branch information
super1207 committed Aug 20, 2024
1 parent 374253c commit 743d560
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
21 changes: 18 additions & 3 deletions src/botconn/kook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,13 @@ impl KookConnect {
cq_add_log_w("要求重连").unwrap();
return Ok(5);
}else if s == 1 {
cq_add_log("连接KOOK成功").unwrap();
cq_add_log_w(&format!("收到KOOK HELLO包:{}", js.to_string())).unwrap();
if js["d"]["code"].as_i64().ok_or("HELLO包错误")? != 0 {
return Ok(1);
} else {
cq_add_log("连接KOOK成功").unwrap();
}
//
}else if s == 3 {
cq_add_log("KOOK心跳接收成功").unwrap();
self.recieve_pong.store(true, std::sync::atomic::Ordering::Relaxed);
Expand All @@ -760,6 +766,8 @@ impl KookConnect {
if rst.is_err() {
cq_add_log_w(&format!("处理KOOK事件出错:{}",rst.err().unwrap())).unwrap();
}
} else {
cq_add_log_w(&format!("KOOK收到未知数据:{}", js.to_string())).unwrap();
}
Ok(0)
}
Expand Down Expand Up @@ -1517,6 +1525,7 @@ impl BotConnectTrait for KookConnect {
self.token = config_json.get("Token").ok_or("kook url格式错误:没有Token")?.as_str().ok_or("kook url格式错误:Token不是字符串")?.to_owned();

let login_info = self.get_login_info().await?;

*self.self_id.write().unwrap() = login_info.user_id;

let wss_url = self.get_gateway().await?;
Expand All @@ -1525,7 +1534,7 @@ impl BotConnectTrait for KookConnect {
let sn_ptr = self.sn.clone();
let is_stop = self.is_stop.clone();
let recieve_pong = self.recieve_pong.clone();
let (stoptx, mut stoprx) = tokio::sync::mpsc::channel::<bool>(1);
let (stoptx, mut stoprx) = tokio::sync::mpsc::channel::<bool>(10);
self.stop_tx = Some(stoptx.clone());
let stop_tx = stoptx.clone();
tokio::spawn(async move {
Expand Down Expand Up @@ -1570,6 +1579,12 @@ impl BotConnectTrait for KookConnect {
// 断开连接
is_stop.store(true, std::sync::atomic::Ordering::Relaxed);
let _foo = stop_tx.send_timeout(true, Duration::from_secs(1)).await;
use crate::botconn::kook::tungstenite::protocol::CloseFrame;
use crate::botconn::kook::tungstenite::protocol::frame::coding::CloseCode::Normal;
let _err = write_halt.send(tungstenite::Message::Close(Some(CloseFrame{
code: Normal,
reason: std::borrow::Cow::Borrowed("byebye")
}))).await;
cq_add_log_w("KOOK心跳断开").unwrap();
});
let is_stop = self.is_stop.clone();
Expand Down Expand Up @@ -1599,7 +1614,7 @@ impl BotConnectTrait for KookConnect {
let kobj = kobj.clone();
let rst = KookConnect::conv_event(&kobj,s).await;
if rst.is_err() {
crate::cqapi::cq_add_log(format!("{:?}", rst.err().unwrap()).as_str()).unwrap();
crate::cqapi::cq_add_log(format!("KOOK抛出错误:{:?}", rst.err().unwrap()).as_str()).unwrap();
} else {
let code = rst.unwrap();
// 断开连接
Expand Down
6 changes: 5 additions & 1 deletion src/botconn/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod qq_guild_all;
mod kook;
mod email;

use std::{collections::HashMap, sync::Arc};
use std::{collections::HashMap, sync::Arc, time::Duration};

use async_trait::async_trait;

Expand Down Expand Up @@ -104,6 +104,10 @@ pub fn do_conn_event() -> Result<i32, Box<dyn std::error::Error>> {
earse_bot[index].write().await.disconnect().await;
G_BOT_MAP.write().await.remove(&earse_urls[index]);
}
// 有bot移除,等30秒再进行连接
if earse_urls.len() > 0 {
tokio::time::sleep(Duration::from_secs(1)).await;
}
}
// 连接未在bot_map中的url
for url in &config_urls {
Expand Down
6 changes: 3 additions & 3 deletions src/httpserver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ async fn connect_handle(request: hyper::Request<hyper::body::Incoming>,is_local:
lk.insert(ws_uid.to_owned(), (tx,platform.clone(),self_id.clone()));
}
if let Err(e) = serve_onebot_websocket(websocket,rx,platform,self_id).await {
cq_add_log_w(&format!("Error in websocket connection: {}", e)).unwrap();
cq_add_log_w(&format!("onebots Error in websocket connection: {}", e)).unwrap();
}
{
let mut lk = G_ONEBOT_WS_MAP.write().await;
Expand Down Expand Up @@ -1067,7 +1067,7 @@ async fn connect_handle(request: hyper::Request<hyper::body::Incoming>,is_local:
lk.insert(uid.clone(), tx);
}
if let Err(e) = serve_websocket(websocket,rx).await {
cq_add_log_w(&format!("Error in websocket connection: {}", e)).unwrap();
cq_add_log_w(&format!("watchlog Error in websocket connection: {}", e)).unwrap();
}
// 线程结束,删除对应的entry
let mut lk = G_LOG_MAP.write().await;
Expand Down Expand Up @@ -1097,7 +1097,7 @@ async fn connect_handle(request: hyper::Request<hyper::body::Incoming>,is_local:
*k = Some(tx);
}
if let Err(e) = serve_py_websocket(websocket,rx).await {
cq_add_log_w(&format!("Error in websocket connection: {}", e)).unwrap();
cq_add_log_w(&format!("pyserver Error in websocket connection: {}", e)).unwrap();
}
});

Expand Down

0 comments on commit 743d560

Please sign in to comment.