From 31e7d2cd59c566c7ae5044d0be44b4e3dcd88082 Mon Sep 17 00:00:00 2001 From: sli97 <775303361@qq.com> Date: Tue, 13 Dec 2022 05:45:44 +0000 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20apps/ser?= =?UTF-8?q?ver/src/core?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/server/src/core/Connection.ts | 97 ------------------------------ apps/server/src/core/index.ts | 2 - 2 files changed, 99 deletions(-) delete mode 100644 apps/server/src/core/Connection.ts delete mode 100644 apps/server/src/core/index.ts diff --git a/apps/server/src/core/Connection.ts b/apps/server/src/core/Connection.ts deleted file mode 100644 index 27a1396..0000000 --- a/apps/server/src/core/Connection.ts +++ /dev/null @@ -1,97 +0,0 @@ -import WebSocket from "ws" -import { EventEmitter } from "stream" -import { MyServer } from "./MyServer" -import { getTime, buffer2ArrayBuffer } from "../Utils" -import { ApiMsgEnum, IModel } from "../Common" -import { binaryEncode, binaryDecode } from "../Common/Binary" - -export enum ConnectionEventEnum { - Close = "Close", -} - -interface IItem { - cb: Function - ctx: unknown -} - -export class Connection extends EventEmitter { - server: MyServer - ws: WebSocket - msgMap: Map> = new Map() - - constructor(server: MyServer, ws: WebSocket) { - super() - - this.server = server - this.ws = ws - this.ws.on("close", (code: number, reason: Buffer) => { - this.emit(ConnectionEventEnum.Close, code, reason.toString()) - }) - - this.ws.on("message", (buffer: Buffer) => { - // const str = buffer.toString() - try { - const json = binaryDecode(buffer2ArrayBuffer(buffer)) - const { name, data } = json - // console.log(`${getTime()}接收|字节数${buffer.length}|${this.playerId || -1}|${str}`) - console.log(`${getTime()}接收|字节数${buffer.length}|${this.playerId || -1}|${JSON.stringify(json)}`) - if (this.server.apiMap.has(name)) { - try { - const cb = this.server.apiMap.get(name) - const res = cb.call(null, this, data) - this.sendMsg(name, { - success: true, - res, - }) - } catch (error) { - this.sendMsg(name, { - success: false, - error: (error as Error)?.message, - }) - } - } else { - try { - if (this.msgMap.has(name)) { - this.msgMap.get(name).forEach(({ cb, ctx }) => cb.call(ctx, this, data)) - } - } catch (error) { - console.log(error) - } - } - } catch (error) { - console.log(`解析失败,不是合法的JSON格式:`, error) - } - }) - } - - listenMsg(name: T, cb: (connection: Connection, arg: IModel["msg"][T]) => void, ctx: unknown) { - if (this.msgMap.has(name)) { - this.msgMap.get(name).push({ cb, ctx }) - } else { - this.msgMap.set(name, [{ cb, ctx }]) - } - } - - unlistenMsg(name: T, cb: (connection: Connection, arg: IModel["msg"][T]) => void, ctx: unknown) { - if (this.msgMap.has(name)) { - const items = this.msgMap.get(name) - const index = items.findIndex((i) => cb === i.cb && i.ctx === ctx) - index > -1 && items.splice(index, 1) - } - } - - sendMsg(name: T, data: IModel["msg"][T]) { - const msg = JSON.stringify({ - name, - data, - }) - const view = binaryEncode(name, data) - const buffer = Buffer.from(view.buffer) - console.log( - `${getTime()}发送|字节数${buffer.length}|${this.playerId || -1}|内存${(process.memoryUsage().heapUsed / 1024 / 1024).toFixed( - 2 - )}MB|${msg}` - ) - this.ws.send(buffer) - } -} diff --git a/apps/server/src/core/index.ts b/apps/server/src/core/index.ts deleted file mode 100644 index d68225a..0000000 --- a/apps/server/src/core/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from "./MyServer"; -export * from "./Connection";