chore: HeartBeat

This commit is contained in:
手瓜一十雪 2024-08-10 16:58:40 +08:00
parent 3269061db4
commit b5d24d751d
6 changed files with 39 additions and 10 deletions

View File

@ -1,5 +1,4 @@
import type { NodeIQQNTWrapperSession, WrapperNodeApi } from "@/core/wrapper/wrapper";
import { randomUUID } from "node:crypto";
import EventEmitter from "node:events";
export type ListenerClassBase = Record<string, string>;

View File

@ -9,6 +9,9 @@ export class OB11ActiveHttpAdapter implements IOB11NetworkAdapter {
constructor(url: string) {
this.url = url;
}
registerHeartBeat() {
//HttpPost心跳
}
registerAction<T extends BaseAction<P, R>, P, R>(action: T) {
this.actionMap.set(action.actionName, action);
@ -22,13 +25,13 @@ export class OB11ActiveHttpAdapter implements IOB11NetworkAdapter {
},
body: JSON.stringify(event)
})
.then(response => response.json())
.then(data => {
console.log('Event sent successfully:', data);
})
.catch(error => {
console.error('Failed to send event:', error);
});
.then(response => response.json())
.then(data => {
console.log('Event sent successfully:', data);
})
.catch(error => {
console.error('Failed to send event:', error);
});
}
async open() {

View File

@ -18,6 +18,10 @@ export class OB11ActiveWebSocketAdapter implements IOB11NetworkAdapter {
this.reconnectIntervalInMillis = reconnectIntervalInMillis;
}
registerHeartBeat() {
//WS反向心跳
}
registerAction<T extends BaseAction<P, R>, P, R>(action: T) {
this.actionMap.set(action.actionName, action);
}

View File

@ -7,3 +7,22 @@ export interface IOB11NetworkAdapter {
open(): void | Promise<void>;
close(): void | Promise<void>;
}
export class OB11NetworkManager {
private adapters: IOB11NetworkAdapter[] = [];
registerAdapter(adapter: IOB11NetworkAdapter) {
this.adapters.push(adapter);
}
async open() {
await Promise.all(this.adapters.map(adapter => adapter.open()));
}
async close() {
await Promise.all(this.adapters.map(adapter => adapter.close()));
}
}
export * from './active-http';
export * from './active-websocket';
export * from './passive-http';
export * from './passive-websocket';

View File

@ -19,7 +19,9 @@ export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter {
registerAction<T extends BaseAction<P, R>, P, R>(action: T) {
this.actionMap.set(action.actionName, action);
}
registerHeartBeat() {
//空心跳
}
onEvent<T extends OB11BaseEvent>(event: T) {
// 事件处理逻辑可以在这里实现
}

View File

@ -48,7 +48,9 @@ export class OB11PassiveWebSocketAdapter implements IOB11NetworkAdapter {
registerAction<T extends BaseAction<P, R>, P, R>(action: T) {
this.actionMap.set(action.actionName, action);
}
registerHeartBeat() {
//WS正向心跳
}
onEvent<T extends OB11BaseEvent>(event: T) {
this.wsClientsMutex.runExclusive(async () => {
this.wsClients.forEach((wsClient) => {