mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2024-11-21 09:36:35 +00:00
chore: network context
This commit is contained in:
parent
505c6e0e0e
commit
9f576f43cc
@ -1,12 +1,18 @@
|
|||||||
import { IOB11NetworkAdapter } from '@/onebot/network/index';
|
import { IOB11NetworkAdapter } from '@/onebot/network/index';
|
||||||
import BaseAction from '@/onebot/action/BaseAction';
|
import BaseAction from '@/onebot/action/BaseAction';
|
||||||
import { OB11BaseEvent } from '@/onebot/event/OB11BaseEvent';
|
import { OB11BaseEvent } from '@/onebot/event/OB11BaseEvent';
|
||||||
|
import { NapCatOneBot11Adapter } from '../main';
|
||||||
|
import { NapCatCore } from '@/core';
|
||||||
|
|
||||||
export class OB11ActiveHttpAdapter implements IOB11NetworkAdapter {
|
export class OB11ActiveHttpAdapter implements IOB11NetworkAdapter {
|
||||||
url: string;
|
url: string;
|
||||||
private actionMap: Map<string, BaseAction<any, any>> = new Map();
|
private actionMap: Map<string, BaseAction<any, any>> = new Map();
|
||||||
|
obContext: NapCatOneBot11Adapter;
|
||||||
|
coreContext: NapCatCore;
|
||||||
|
|
||||||
constructor(url: string) {
|
constructor(obContext: NapCatOneBot11Adapter, coreContext: NapCatCore, url: string) {
|
||||||
|
this.obContext = obContext;
|
||||||
|
this.coreContext = coreContext
|
||||||
this.url = url;
|
this.url = url;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -22,13 +28,13 @@ export class OB11ActiveHttpAdapter implements IOB11NetworkAdapter {
|
|||||||
},
|
},
|
||||||
body: JSON.stringify(event)
|
body: JSON.stringify(event)
|
||||||
})
|
})
|
||||||
.then(response => response.json())
|
.then(response => response.json())
|
||||||
.then(data => {
|
.then(data => {
|
||||||
console.log('Event sent successfully:', data);
|
console.log('Event sent successfully:', data);
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
console.error('Failed to send event:', error);
|
console.error('Failed to send event:', error);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async open() {
|
async open() {
|
||||||
|
@ -4,6 +4,8 @@ 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 { OB11BaseEvent } from '@/onebot/event/OB11BaseEvent';
|
||||||
import { sleep } from '@/common/utils/helper';
|
import { sleep } from '@/common/utils/helper';
|
||||||
|
import { NapCatOneBot11Adapter } from '../main';
|
||||||
|
import { NapCatCore } from '@/core';
|
||||||
|
|
||||||
export class OB11ActiveWebSocketAdapter implements IOB11NetworkAdapter {
|
export class OB11ActiveWebSocketAdapter implements IOB11NetworkAdapter {
|
||||||
url: string;
|
url: string;
|
||||||
@ -12,8 +14,12 @@ export class OB11ActiveWebSocketAdapter implements IOB11NetworkAdapter {
|
|||||||
|
|
||||||
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();
|
||||||
|
obContext: NapCatOneBot11Adapter;
|
||||||
|
coreContext: NapCatCore;
|
||||||
|
|
||||||
constructor(url: string, reconnectIntervalInMillis: number) {
|
constructor(obContext: NapCatOneBot11Adapter, coreContext: NapCatCore, url: string, reconnectIntervalInMillis: number) {
|
||||||
|
this.obContext = obContext;
|
||||||
|
this.coreContext = coreContext
|
||||||
this.url = url;
|
this.url = url;
|
||||||
this.reconnectIntervalInMillis = reconnectIntervalInMillis;
|
this.reconnectIntervalInMillis = reconnectIntervalInMillis;
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,8 @@ import { OB11BaseEvent } from '@/onebot/event/OB11BaseEvent';
|
|||||||
import BaseAction from '@/onebot/action/BaseAction';
|
import BaseAction from '@/onebot/action/BaseAction';
|
||||||
import express, { Express, Request, Response } from 'express';
|
import express, { Express, Request, Response } from 'express';
|
||||||
import http from 'http';
|
import http from 'http';
|
||||||
|
import { NapCatOneBot11Adapter } from '../main';
|
||||||
|
import { NapCatCore } from '@/core';
|
||||||
|
|
||||||
export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter {
|
export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter {
|
||||||
private app: Express | undefined;
|
private app: Express | undefined;
|
||||||
@ -11,8 +13,12 @@ export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter {
|
|||||||
private hasBeenClosed: boolean = false;
|
private hasBeenClosed: boolean = false;
|
||||||
private actionMap: Map<string, BaseAction<any, any>> = new Map();
|
private actionMap: Map<string, BaseAction<any, any>> = new Map();
|
||||||
private port: number;
|
private port: number;
|
||||||
|
obContext: NapCatOneBot11Adapter;
|
||||||
|
coreContext: NapCatCore;
|
||||||
|
|
||||||
constructor(port: number) {
|
constructor(obContext: NapCatOneBot11Adapter, coreContext: NapCatCore, port: number) {
|
||||||
|
this.obContext = obContext;
|
||||||
|
this.coreContext = coreContext
|
||||||
this.port = port;
|
this.port = port;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,6 +3,8 @@ import { OB11BaseEvent } from '@/onebot/event/OB11BaseEvent';
|
|||||||
import BaseAction from '@/onebot/action/BaseAction';
|
import BaseAction from '@/onebot/action/BaseAction';
|
||||||
import { WebSocket, WebSocketServer } from 'ws';
|
import { WebSocket, WebSocketServer } from 'ws';
|
||||||
import { Mutex } from 'async-mutex';
|
import { Mutex } from 'async-mutex';
|
||||||
|
import { NapCatOneBot11Adapter } from '../main';
|
||||||
|
import { NapCatCore } from '@/core';
|
||||||
|
|
||||||
export class OB11PassiveWebSocketAdapter implements IOB11NetworkAdapter {
|
export class OB11PassiveWebSocketAdapter implements IOB11NetworkAdapter {
|
||||||
wsServer: WebSocketServer;
|
wsServer: WebSocketServer;
|
||||||
@ -10,10 +12,13 @@ export class OB11PassiveWebSocketAdapter implements IOB11NetworkAdapter {
|
|||||||
wsClientsMutex = new Mutex();
|
wsClientsMutex = new Mutex();
|
||||||
isOpen: boolean = false;
|
isOpen: boolean = false;
|
||||||
hasBeenClosed: boolean = false;
|
hasBeenClosed: boolean = false;
|
||||||
|
obContext: NapCatOneBot11Adapter;
|
||||||
|
coreContext: NapCatCore;
|
||||||
private actionMap: Map<string, BaseAction<any, any>> = new Map();
|
private actionMap: Map<string, BaseAction<any, any>> = new Map();
|
||||||
|
|
||||||
constructor(ip: string, port: number, token: string) {
|
constructor(obContext: NapCatOneBot11Adapter, coreContext: NapCatCore,ip: string, port: number, token: string) {
|
||||||
|
this.obContext = obContext;
|
||||||
|
this.coreContext = coreContext
|
||||||
this.wsServer = new WebSocketServer({ port: port, host: ip });
|
this.wsServer = new WebSocketServer({ port: port, host: ip });
|
||||||
this.wsServer.on('connection', async (wsClient) => {
|
this.wsServer.on('connection', async (wsClient) => {
|
||||||
if (!this.isOpen) {
|
if (!this.isOpen) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user