mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2026-02-13 00:10:27 +00:00
refactor: core
This commit is contained in:
@@ -3,7 +3,7 @@ import { OB11BaseMetaEvent } from '../event/meta/OB11BaseMetaEvent';
|
||||
import { OB11BaseNoticeEvent } from '../event/notice/OB11BaseNoticeEvent';
|
||||
import { WebSocket as WebSocketClass } from 'ws';
|
||||
import { wsReply } from './ws/reply';
|
||||
import { log } from '@/common/utils/log';
|
||||
import { log, logDebug, logError } from '@/common/utils/log';
|
||||
import { ob11Config } from '@/onebot11/config';
|
||||
import crypto from 'crypto';
|
||||
import { ChatType, Group, GroupRequestOperateTypes, Peer } from '../../core/src/entities';
|
||||
@@ -95,14 +95,14 @@ export function postOB11Event(msg: PostEventType, reportSelf = false, postWs= tr
|
||||
headers,
|
||||
body: msgStr
|
||||
}).then(async (res) => {
|
||||
log(`新消息事件HTTP上报成功: ${host} `, msgStr);
|
||||
logDebug(`新消息事件HTTP上报成功: ${host} `, msgStr);
|
||||
// todo: 处理不够优雅,应该使用高级泛型进行QuickAction类型识别
|
||||
let resJson: QuickAction;
|
||||
try {
|
||||
resJson = await res.json();
|
||||
log('新消息事件HTTP上报返回快速操作: ', JSON.stringify(resJson));
|
||||
logDebug('新消息事件HTTP上报返回快速操作: ', JSON.stringify(resJson));
|
||||
} catch (e) {
|
||||
log('新消息事件HTTP上报没有返回快速操作,不需要处理');
|
||||
logDebug('新消息事件HTTP上报没有返回快速操作,不需要处理');
|
||||
return;
|
||||
}
|
||||
if (msg.post_type === 'message') {
|
||||
@@ -177,7 +177,7 @@ export function postOB11Event(msg: PostEventType, reportSelf = false, postWs= tr
|
||||
}
|
||||
}
|
||||
}, (err: any) => {
|
||||
log(`新消息事件HTTP上报失败: ${host} `, err, msg);
|
||||
logError(`新消息事件HTTP上报失败: ${host} `, err, msg);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import { postWsEvent, registerWsEventSender, unregisterWsEventSender } from '../
|
||||
import { wsReply } from './reply';
|
||||
import { WebSocket as WebSocketClass } from 'ws';
|
||||
import { OB11HeartbeatEvent } from '../../event/meta/OB11HeartbeatEvent';
|
||||
import { log } from '../../../common/utils/log';
|
||||
import { log, logDebug, logError } from '../../../common/utils/log';
|
||||
import { ob11Config } from '@/onebot11/config';
|
||||
import { napCatCore } from '@/core';
|
||||
import { selfInfo } from '@/common/data';
|
||||
@@ -40,7 +40,7 @@ export class ReverseWebsocket {
|
||||
try {
|
||||
receiveData = JSON.parse(msg.toString());
|
||||
echo = receiveData.echo;
|
||||
log('收到反向Websocket消息', receiveData);
|
||||
logDebug('收到反向Websocket消息', receiveData);
|
||||
} catch (e) {
|
||||
return wsReply(this.websocket!, OB11Response.error('json解析失败,请检查数据格式', 1400, echo));
|
||||
}
|
||||
@@ -57,7 +57,7 @@ export class ReverseWebsocket {
|
||||
}
|
||||
|
||||
public onclose = () => {
|
||||
log('反向ws断开', this.url);
|
||||
logDebug('反向ws断开', this.url);
|
||||
unregisterWsEventSender(this.websocket!);
|
||||
if (this.running) {
|
||||
this.reconnect();
|
||||
@@ -89,11 +89,11 @@ export class ReverseWebsocket {
|
||||
}
|
||||
});
|
||||
registerWsEventSender(this.websocket);
|
||||
log('Trying to connect to the websocket server: ' + this.url);
|
||||
logDebug('Trying to connect to the websocket server: ' + this.url);
|
||||
|
||||
|
||||
this.websocket.on('open', () => {
|
||||
log('Connected to the websocket server: ' + this.url);
|
||||
logDebug('Connected to the websocket server: ' + this.url);
|
||||
this.onopen();
|
||||
});
|
||||
|
||||
@@ -108,7 +108,7 @@ export class ReverseWebsocket {
|
||||
}, heartInterval); // 心跳包
|
||||
this.websocket.on('close', () => {
|
||||
clearInterval(wsClientInterval);
|
||||
log('The websocket connection: ' + this.url + ' closed, trying reconnecting...');
|
||||
logDebug('The websocket connection: ' + this.url + ' closed, trying reconnecting...');
|
||||
this.onclose();
|
||||
});
|
||||
}
|
||||
@@ -122,7 +122,7 @@ class OB11ReverseWebsockets {
|
||||
try {
|
||||
rwsList.push(new ReverseWebsocket(url));
|
||||
} catch (e: any) {
|
||||
log(e.stack);
|
||||
logError(e.stack);
|
||||
}
|
||||
}).then();
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ import { WebsocketServerBase } from '@/common/server/websocket';
|
||||
import { IncomingMessage } from 'node:http';
|
||||
import { wsReply } from './reply';
|
||||
import { napCatCore } from '@/core';
|
||||
import { log } from '../../../common/utils/log';
|
||||
import { log, logDebug, logError } from '../../../common/utils/log';
|
||||
import { ob11Config } from '@/onebot11/config';
|
||||
import { selfInfo } from '@/common/data';
|
||||
|
||||
@@ -58,19 +58,19 @@ class OB11WebsocketServer extends WebsocketServerBase {
|
||||
if (url == '/event' || url == '/event/' || url == '/') {
|
||||
registerWsEventSender(wsClient);
|
||||
|
||||
log('event上报ws客户端已连接');
|
||||
logDebug('event上报ws客户端已连接');
|
||||
|
||||
try {
|
||||
wsReply(wsClient, new OB11LifeCycleEvent(LifeCycleSubType.CONNECT));
|
||||
} catch (e) {
|
||||
log('发送生命周期失败', e);
|
||||
logError('发送生命周期失败', e);
|
||||
}
|
||||
const { heartInterval } = ob11Config;
|
||||
const wsClientInterval = setInterval(() => {
|
||||
postWsEvent(new OB11HeartbeatEvent(!!selfInfo.online, true, heartInterval));
|
||||
}, heartInterval); // 心跳包
|
||||
wsClient.on('close', () => {
|
||||
log('event上报ws客户端已断开');
|
||||
logError('event上报ws客户端已断开');
|
||||
clearInterval(wsClientInterval);
|
||||
unregisterWsEventSender(wsClient);
|
||||
});
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { WebSocket as WebSocketClass } from 'ws';
|
||||
import { OB11Response } from '../../action/OB11Response';
|
||||
import { PostEventType } from '../postOB11Event';
|
||||
import { log } from '../../../common/utils/log';
|
||||
import { log, logDebug, logError } from '../../../common/utils/log';
|
||||
import { isNull } from '../../../common/utils/helper';
|
||||
|
||||
|
||||
@@ -12,8 +12,8 @@ export function wsReply(wsClient: WebSocketClass, data: OB11Response | PostEvent
|
||||
delete packet['echo'];
|
||||
}
|
||||
wsClient.send(JSON.stringify(packet));
|
||||
log('ws 消息上报', wsClient.url || '', data);
|
||||
logDebug('ws 消息上报', wsClient.url || '', data);
|
||||
} catch (e: any) {
|
||||
log('websocket 回复失败', e.stack, data);
|
||||
logError('websocket 回复失败', e.stack, data);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user