From 28ffd134dfef4d3c1c935269bcfe07501bf54d86 Mon Sep 17 00:00:00 2001 From: CrescentLeaf Date: Sun, 21 Sep 2025 14:12:06 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9C=8D=E5=8B=99=E7=AB=AF=20Api=20?= =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E6=8C=81=E6=9C=89=20client=20socket?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/api/ApiManager.ts | 10 +++++----- server/api/UserApi.ts | 22 ++++++++++++++++------ server/typedef/EventCallbackFunction.ts | 2 ++ 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/server/api/ApiManager.ts b/server/api/ApiManager.ts index e695a61..3b29cc9 100644 --- a/server/api/ApiManager.ts +++ b/server/api/ApiManager.ts @@ -32,9 +32,9 @@ export default class ApiManager { static addEventListener(name: string, func: EventCallbackFunction) { this.event_listeners[name] = func } - static clients: { [key: string]: string[] } = {} + static clients: { [key: string]: { [key: string]: SocketIo.Socket } } = {} static checkUserIsOnline(userId: string, deviceId: string) { - return this.clients[userId].includes(deviceId) + return this.clients[userId]?.[deviceId] != null } static initEvents() { const io = this.socketIoServer @@ -49,7 +49,8 @@ export default class ApiManager { const clientInfo = { userId: '', deviceId, - ip + ip, + socket, } socket.on('disconnect', (_reason) => { @@ -57,8 +58,7 @@ export default class ApiManager { console.log(chalk.yellow('[斷]') + ` ${ip} disconnected`) else { console.log(chalk.green('[斷]') + ` ${ip} disconnected`) - const ls = this.clients[clientInfo.userId] - ls.splice(ls.indexOf(deviceId)) + delete this.clients[clientInfo.userId][deviceId] } }) console.log(chalk.yellow('[連]') + ` ${ip} connected`) diff --git a/server/api/UserApi.ts b/server/api/UserApi.ts index ab8d854..0a81020 100644 --- a/server/api/UserApi.ts +++ b/server/api/UserApi.ts @@ -1,9 +1,11 @@ -import { Buffer } from "node:buffer"; -import User from "../data/User.ts"; +import { Buffer } from "node:buffer" +import User from "../data/User.ts" import BaseApi from "./BaseApi.ts" -import TokenManager from "./TokenManager.ts"; -import ChatPrivate from "../data/ChatPrivate.ts"; -import Chat from "../data/Chat.ts"; +import TokenManager from "./TokenManager.ts" +import ChatPrivate from "../data/ChatPrivate.ts" +import Chat from "../data/Chat.ts" +import chalk from "chalk" +import ApiManager from "./ApiManager.ts" export default class UserApi extends BaseApi { override getName(): string { @@ -11,11 +13,12 @@ export default class UserApi extends BaseApi { } override onInit(): void { // 驗證 - this.registerEvent("User.auth", (args, { deviceId }) => { + this.registerEvent("User.auth", (args, clientInfo) => { if (this.checkArgsMissing(args, ['access_token'])) return { msg: "參數缺失", code: 400, } + const { deviceId, ip, socket } = clientInfo try { const access_token = TokenManager.decode(args.access_token as string) @@ -31,6 +34,13 @@ export default class UserApi extends BaseApi { msg: "驗證失敗", code: 401, } + + clientInfo.userId = access_token.author + console.log(chalk.green('[驗]') + ` ${access_token.author} authed on Client ${deviceId} (ip = ${ip})`) + if (ApiManager.clients[clientInfo.userId] == null) ApiManager.clients[clientInfo.userId] = { + [deviceId]: socket + } + else ApiManager.clients[clientInfo.userId][deviceId] = socket return { msg: "成功", diff --git a/server/typedef/EventCallbackFunction.ts b/server/typedef/EventCallbackFunction.ts index 8db1926..3584d2e 100644 --- a/server/typedef/EventCallbackFunction.ts +++ b/server/typedef/EventCallbackFunction.ts @@ -1,9 +1,11 @@ import ApiCallbackMessage from "../api/ApiCallbackMessage.ts" +import * as SocketIo from "socket.io" type EventCallbackFunction = (args: { [key: string]: unknown }, clientInfo: { userId: string deviceId: string ip: string + socket: SocketIo.Socket }) => ApiCallbackMessage export default EventCallbackFunction