mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2024-11-21 09:36:35 +00:00
chore: HttpServer
This commit is contained in:
parent
7aa01f786d
commit
8780c987ea
@ -1,50 +1,19 @@
|
|||||||
import { IOB11NetworkAdapter } from './index';
|
import { IOB11NetworkAdapter } from './index';
|
||||||
import { OB11BaseEvent } from '@/onebot/event/OB11BaseEvent';
|
import { OB11BaseEvent } from '@/onebot/event/OB11BaseEvent';
|
||||||
import BaseAction from '@/onebot/action/BaseAction';
|
import BaseAction from '@/onebot/action/BaseAction';
|
||||||
import { Mutex } from 'async-mutex';
|
|
||||||
import express, { Express, Request, Response } from 'express';
|
import express, { Express, Request, Response } from 'express';
|
||||||
import http from 'http';
|
import http from 'http';
|
||||||
|
|
||||||
export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter {
|
export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter {
|
||||||
private app: Express;
|
private app: Express | undefined;
|
||||||
private server: http.Server;
|
private server: http.Server | undefined;
|
||||||
private clients: { res: Response }[] = [];
|
|
||||||
private clientsMutex = new Mutex();
|
|
||||||
private isOpen: boolean = false;
|
private isOpen: boolean = false;
|
||||||
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;
|
||||||
|
|
||||||
constructor(port: number) {
|
constructor(port: number) {
|
||||||
this.app = express();
|
this.port = port;
|
||||||
this.server = http.createServer(this.app);
|
|
||||||
|
|
||||||
this.app.use(express.json());
|
|
||||||
|
|
||||||
this.app.post('/action', async (req: Request, res: Response) => {
|
|
||||||
if (!this.isOpen) {
|
|
||||||
res.status(503).send('Server is closed');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const { actionName, payload } = req.body;
|
|
||||||
const action = this.actionMap.get(actionName);
|
|
||||||
if (action) {
|
|
||||||
const result = await action.handle(payload);
|
|
||||||
res.json(result);
|
|
||||||
} else {
|
|
||||||
res.status(404).send('Action not found');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
this.app.post('/event', (req: Request, res: Response) => {
|
|
||||||
this.clientsMutex.runExclusive(async () => {
|
|
||||||
this.clients.push({ res });
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
this.server.listen(port, () => {
|
|
||||||
console.log(`HTTP server listening on port ${port}`);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
registerAction<T extends BaseAction<P, R>, P, R>(action: T) {
|
registerAction<T extends BaseAction<P, R>, P, R>(action: T) {
|
||||||
@ -52,24 +21,55 @@ export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onEvent<T extends OB11BaseEvent>(event: T) {
|
onEvent<T extends OB11BaseEvent>(event: T) {
|
||||||
this.clientsMutex.runExclusive(async () => {
|
// 事件处理逻辑可以在这里实现
|
||||||
this.clients.forEach(({ res }) => {
|
|
||||||
res.json(event);
|
|
||||||
});
|
|
||||||
this.clients = [];
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
open() {
|
open() {
|
||||||
if (this.hasBeenClosed) {
|
if (this.hasBeenClosed) {
|
||||||
throw new Error('Cannot open a closed HTTP server');
|
throw new Error('Cannot open a closed HTTP server');
|
||||||
}
|
}
|
||||||
this.isOpen = true;
|
if (!this.isOpen) {
|
||||||
|
this.initializeServer();
|
||||||
|
this.isOpen = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private initializeServer() {
|
||||||
|
this.app = express();
|
||||||
|
this.server = http.createServer(this.app);
|
||||||
|
|
||||||
|
this.app.use(express.json());
|
||||||
|
|
||||||
|
this.app.all('*', this.handleRequest.bind(this));
|
||||||
|
|
||||||
|
this.server.listen(this.port, () => {
|
||||||
|
console.log(`HTTP server listening on port ${this.port}`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private async handleRequest(req: Request, res: Response) {
|
||||||
|
if (!this.isOpen) {
|
||||||
|
res.status(503).send('Server is closed');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const actionName = req.path.split('/')[1];
|
||||||
|
const action = this.actionMap.get(actionName);
|
||||||
|
if (action) {
|
||||||
|
try {
|
||||||
|
const result = await action.handle(req.body);
|
||||||
|
res.json(result);
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).send('Internal Server Error');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
res.status(404).send('Action not found');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async close() {
|
async close() {
|
||||||
this.isOpen = false;
|
this.isOpen = false;
|
||||||
this.hasBeenClosed = true;
|
this.hasBeenClosed = true;
|
||||||
this.server.close();
|
this.server?.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user