feat: 服務端 Api 可以持有 client socket
This commit is contained in:
@@ -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<SocketIo.DefaultEventsMap, SocketIo.DefaultEventsMap, SocketIo.DefaultEventsMap, any> } } = {}
|
||||
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`)
|
||||
|
||||
@@ -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: "成功",
|
||||
|
||||
@@ -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<SocketIo.DefaultEventsMap, SocketIo.DefaultEventsMap, SocketIo.DefaultEventsMap, any>
|
||||
}) => ApiCallbackMessage
|
||||
|
||||
export default EventCallbackFunction
|
||||
|
||||
Reference in New Issue
Block a user