From 8780c987ea4598f099c5f176675f087e56c68b31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Sat, 10 Aug 2024 16:09:26 +0800 Subject: [PATCH] chore: HttpServer --- src/onebot/network/passive-http.ts | 86 +++++++++++++++--------------- 1 file changed, 43 insertions(+), 43 deletions(-) diff --git a/src/onebot/network/passive-http.ts b/src/onebot/network/passive-http.ts index 960a4d60..8f28208a 100644 --- a/src/onebot/network/passive-http.ts +++ b/src/onebot/network/passive-http.ts @@ -1,50 +1,19 @@ import { IOB11NetworkAdapter } from './index'; import { OB11BaseEvent } from '@/onebot/event/OB11BaseEvent'; import BaseAction from '@/onebot/action/BaseAction'; -import { Mutex } from 'async-mutex'; import express, { Express, Request, Response } from 'express'; import http from 'http'; export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter { - private app: Express; - private server: http.Server; - private clients: { res: Response }[] = []; - private clientsMutex = new Mutex(); + private app: Express | undefined; + private server: http.Server | undefined; private isOpen: boolean = false; private hasBeenClosed: boolean = false; private actionMap: Map> = new Map(); + private port: number; constructor(port: number) { - this.app = express(); - 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}`); - }); + this.port = port; } registerAction, P, R>(action: T) { @@ -52,24 +21,55 @@ export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter { } onEvent(event: T) { - this.clientsMutex.runExclusive(async () => { - this.clients.forEach(({ res }) => { - res.json(event); - }); - this.clients = []; - }); + // 事件处理逻辑可以在这里实现 } open() { if (this.hasBeenClosed) { 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() { this.isOpen = false; this.hasBeenClosed = true; - this.server.close(); + this.server?.close(); } } \ No newline at end of file