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