mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2024-11-21 09:36:35 +00:00
chore: websocket
This commit is contained in:
parent
832635d6f5
commit
a2ad39f78d
@ -1,18 +1,16 @@
|
|||||||
import { IOB11NetworkAdapter, OB11EmitEventContent } from '@/onebot/network/index';
|
import { IOB11NetworkAdapter, OB11EmitEventContent } from '@/onebot/network/index';
|
||||||
|
|
||||||
import { WebSocket as NodeWebSocket } from 'ws';
|
import { WebSocket as NodeWebSocket } from 'ws';
|
||||||
import BaseAction from '@/onebot/action/BaseAction';
|
import BaseAction from '@/onebot/action/BaseAction';
|
||||||
import { OB11BaseEvent } from '@/onebot/event/OB11BaseEvent';
|
|
||||||
import { sleep } from '@/common/utils/helper';
|
import { sleep } from '@/common/utils/helper';
|
||||||
|
|
||||||
export class OB11ActiveWebSocketAdapter implements IOB11NetworkAdapter {
|
export class OB11ActiveWebSocketAdapter implements IOB11NetworkAdapter {
|
||||||
url: string;
|
url: string;
|
||||||
reconnectIntervalInMillis: number;
|
reconnectIntervalInMillis: number;
|
||||||
isClosed: boolean = false;
|
isClosed: boolean = false;
|
||||||
|
|
||||||
private connection: NodeWebSocket | null = null;
|
private connection: NodeWebSocket | null = null;
|
||||||
private actionMap: Map<string, BaseAction<any, any>> = new Map();
|
private actionMap: Map<string, BaseAction<any, any>> = new Map();
|
||||||
heartbeatInterval: number;
|
heartbeatInterval: number;
|
||||||
|
private heartbeatTimer: NodeJS.Timeout | null = null;
|
||||||
|
|
||||||
constructor(url: string, reconnectIntervalInMillis: number, heartbeatInterval: number) {
|
constructor(url: string, reconnectIntervalInMillis: number, heartbeatInterval: number) {
|
||||||
this.url = url;
|
this.url = url;
|
||||||
@ -21,7 +19,14 @@ export class OB11ActiveWebSocketAdapter implements IOB11NetworkAdapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
registerHeartBeat() {
|
registerHeartBeat() {
|
||||||
//WS反向心跳
|
// WS反向心跳
|
||||||
|
if (this.connection) {
|
||||||
|
this.heartbeatTimer = setInterval(() => {
|
||||||
|
if (this.connection && this.connection.readyState === NodeWebSocket.OPEN) {
|
||||||
|
this.connection.ping();
|
||||||
|
}
|
||||||
|
}, this.heartbeatInterval);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
registerAction<T extends BaseAction<P, R>, P, R>(action: T) {
|
registerAction<T extends BaseAction<P, R>, P, R>(action: T) {
|
||||||
@ -30,9 +35,8 @@ export class OB11ActiveWebSocketAdapter implements IOB11NetworkAdapter {
|
|||||||
|
|
||||||
onEvent<T extends OB11EmitEventContent>(event: T) {
|
onEvent<T extends OB11EmitEventContent>(event: T) {
|
||||||
if (this.connection) {
|
if (this.connection) {
|
||||||
// this.connection.send(JSON.stringify(event));
|
const wrappedEvent = this.wrapEvent(event);
|
||||||
// TODO: wrap the event, and send the wrapped to the server.
|
this.connection.send(JSON.stringify(wrappedEvent));
|
||||||
// TODO: consider using a utility function
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,37 +48,59 @@ export class OB11ActiveWebSocketAdapter implements IOB11NetworkAdapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
close() {
|
close() {
|
||||||
this.isClosed = true;
|
|
||||||
if (this.isClosed) {
|
if (this.isClosed) {
|
||||||
throw new Error('Cannot close a closed WebSocket connection');
|
throw new Error('Cannot close a closed WebSocket connection');
|
||||||
}
|
}
|
||||||
|
this.isClosed = true;
|
||||||
if (this.connection) {
|
if (this.connection) {
|
||||||
this.connection.close();
|
this.connection.close();
|
||||||
this.connection = null;
|
this.connection = null;
|
||||||
}
|
}
|
||||||
|
if (this.heartbeatTimer) {
|
||||||
|
clearInterval(this.heartbeatTimer);
|
||||||
|
this.heartbeatTimer = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async tryConnect() {
|
private async tryConnect() {
|
||||||
while (!this.connection) {
|
while (!this.connection) {
|
||||||
try {
|
try {
|
||||||
this.connection = new NodeWebSocket(this.url);
|
this.connection = new NodeWebSocket(this.url);
|
||||||
|
this.connection.on('message', (data) => {
|
||||||
|
this.handleMessage(data);
|
||||||
|
});
|
||||||
|
this.connection.once('close', () => {
|
||||||
|
if (!this.isClosed) {
|
||||||
|
this.connection = null;
|
||||||
|
setTimeout(() => this.tryConnect(), this.reconnectIntervalInMillis);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
this.registerHeartBeat();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.connection = null;
|
this.connection = null;
|
||||||
console.error('Failed to connect to the server, retrying in 5 seconds...');
|
console.error('Failed to connect to the server, retrying in 5 seconds...');
|
||||||
await sleep(5000);
|
await sleep(5000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.connection.on('message', (data) => {
|
|
||||||
// TODO: extract action name and payload from the message, then call the corresponding action.
|
|
||||||
// TODO: consider using a utility function
|
|
||||||
});
|
|
||||||
|
|
||||||
this.connection.once('close', () => {
|
|
||||||
if (!this.isClosed) {
|
|
||||||
this.connection = new NodeWebSocket(this.url);
|
|
||||||
this.tryConnect();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
private handleMessage(data: any) {
|
||||||
|
try {
|
||||||
|
const message = JSON.parse(data);
|
||||||
|
const action = this.actionMap.get(message.actionName);
|
||||||
|
if (action) {
|
||||||
|
action.handle(message.payload);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Failed to handle message:', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private wrapEvent<T extends OB11EmitEventContent>(event: T) {
|
||||||
|
// Wrap the event as needed
|
||||||
|
return {
|
||||||
|
type: 'event',
|
||||||
|
data: event
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user