feat: 服務端 Api 可以持有 client socket
This commit is contained in:
@@ -32,9 +32,9 @@ export default class ApiManager {
|
|||||||
static addEventListener(name: string, func: EventCallbackFunction) {
|
static addEventListener(name: string, func: EventCallbackFunction) {
|
||||||
this.event_listeners[name] = func
|
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) {
|
static checkUserIsOnline(userId: string, deviceId: string) {
|
||||||
return this.clients[userId].includes(deviceId)
|
return this.clients[userId]?.[deviceId] != null
|
||||||
}
|
}
|
||||||
static initEvents() {
|
static initEvents() {
|
||||||
const io = this.socketIoServer
|
const io = this.socketIoServer
|
||||||
@@ -49,7 +49,8 @@ export default class ApiManager {
|
|||||||
const clientInfo = {
|
const clientInfo = {
|
||||||
userId: '',
|
userId: '',
|
||||||
deviceId,
|
deviceId,
|
||||||
ip
|
ip,
|
||||||
|
socket,
|
||||||
}
|
}
|
||||||
|
|
||||||
socket.on('disconnect', (_reason) => {
|
socket.on('disconnect', (_reason) => {
|
||||||
@@ -57,8 +58,7 @@ export default class ApiManager {
|
|||||||
console.log(chalk.yellow('[斷]') + ` ${ip} disconnected`)
|
console.log(chalk.yellow('[斷]') + ` ${ip} disconnected`)
|
||||||
else {
|
else {
|
||||||
console.log(chalk.green('[斷]') + ` ${ip} disconnected`)
|
console.log(chalk.green('[斷]') + ` ${ip} disconnected`)
|
||||||
const ls = this.clients[clientInfo.userId]
|
delete this.clients[clientInfo.userId][deviceId]
|
||||||
ls.splice(ls.indexOf(deviceId))
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
console.log(chalk.yellow('[連]') + ` ${ip} connected`)
|
console.log(chalk.yellow('[連]') + ` ${ip} connected`)
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
import { Buffer } from "node:buffer";
|
import { Buffer } from "node:buffer"
|
||||||
import User from "../data/User.ts";
|
import User from "../data/User.ts"
|
||||||
import BaseApi from "./BaseApi.ts"
|
import BaseApi from "./BaseApi.ts"
|
||||||
import TokenManager from "./TokenManager.ts";
|
import TokenManager from "./TokenManager.ts"
|
||||||
import ChatPrivate from "../data/ChatPrivate.ts";
|
import ChatPrivate from "../data/ChatPrivate.ts"
|
||||||
import Chat from "../data/Chat.ts";
|
import Chat from "../data/Chat.ts"
|
||||||
|
import chalk from "chalk"
|
||||||
|
import ApiManager from "./ApiManager.ts"
|
||||||
|
|
||||||
export default class UserApi extends BaseApi {
|
export default class UserApi extends BaseApi {
|
||||||
override getName(): string {
|
override getName(): string {
|
||||||
@@ -11,11 +13,12 @@ export default class UserApi extends BaseApi {
|
|||||||
}
|
}
|
||||||
override onInit(): void {
|
override onInit(): void {
|
||||||
// 驗證
|
// 驗證
|
||||||
this.registerEvent("User.auth", (args, { deviceId }) => {
|
this.registerEvent("User.auth", (args, clientInfo) => {
|
||||||
if (this.checkArgsMissing(args, ['access_token'])) return {
|
if (this.checkArgsMissing(args, ['access_token'])) return {
|
||||||
msg: "參數缺失",
|
msg: "參數缺失",
|
||||||
code: 400,
|
code: 400,
|
||||||
}
|
}
|
||||||
|
const { deviceId, ip, socket } = clientInfo
|
||||||
try {
|
try {
|
||||||
const access_token = TokenManager.decode(args.access_token as string)
|
const access_token = TokenManager.decode(args.access_token as string)
|
||||||
|
|
||||||
@@ -31,6 +34,13 @@ export default class UserApi extends BaseApi {
|
|||||||
msg: "驗證失敗",
|
msg: "驗證失敗",
|
||||||
code: 401,
|
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 {
|
return {
|
||||||
msg: "成功",
|
msg: "成功",
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
import ApiCallbackMessage from "../api/ApiCallbackMessage.ts"
|
import ApiCallbackMessage from "../api/ApiCallbackMessage.ts"
|
||||||
|
import * as SocketIo from "socket.io"
|
||||||
|
|
||||||
type EventCallbackFunction = (args: { [key: string]: unknown }, clientInfo: {
|
type EventCallbackFunction = (args: { [key: string]: unknown }, clientInfo: {
|
||||||
userId: string
|
userId: string
|
||||||
deviceId: string
|
deviceId: string
|
||||||
ip: string
|
ip: string
|
||||||
|
socket: SocketIo.Socket<SocketIo.DefaultEventsMap, SocketIo.DefaultEventsMap, SocketIo.DefaultEventsMap, any>
|
||||||
}) => ApiCallbackMessage
|
}) => ApiCallbackMessage
|
||||||
|
|
||||||
export default EventCallbackFunction
|
export default EventCallbackFunction
|
||||||
|
|||||||
Reference in New Issue
Block a user