fix: 多個同 DeviceId 不同 Session 的客戶端無法同時收到消息

This commit is contained in:
CrescentLeaf
2025-09-24 22:03:23 +08:00
parent 9a3e87d89c
commit 38db2e1310
5 changed files with 15 additions and 8 deletions

View File

@@ -9,6 +9,7 @@ import randomUUID from "../randomUUID.ts"
type UnknownObject = { [key: string]: unknown } type UnknownObject = { [key: string]: unknown }
class Client { class Client {
static sessionId = randomUUID()
static myUserProfile?: User static myUserProfile?: User
static socket?: Socket static socket?: Socket
static events: { [key: string]: (data: UnknownObject) => UnknownObject | void } = {} static events: { [key: string]: (data: UnknownObject) => UnknownObject | void } = {}
@@ -21,7 +22,8 @@ class Client {
this.socket = io({ this.socket = io({
transports: ['websocket'], transports: ['websocket'],
auth: { auth: {
device_id: data.device_id device_id: data.device_id,
session_id: this.sessionId,
}, },
}) })
this.socket!.on("connect", async () => { this.socket!.on("connect", async () => {

View File

@@ -48,6 +48,9 @@ export default class ApiManager {
static checkUserIsOnline(userId: string) { static checkUserIsOnline(userId: string) {
return this.getUserClientSockets(userId) != null return this.getUserClientSockets(userId) != null
} }
/**
* 獲取用戶所有的客戶端表 (格式遵循 設備ID_當前Session)
*/
static getUserClientSockets(userId: string) { static getUserClientSockets(userId: string) {
return this.clients[userId] return this.clients[userId]
} }
@@ -60,10 +63,12 @@ export default class ApiManager {
const ip = socket.conn.remoteAddress const ip = socket.conn.remoteAddress
const deviceId = socket.handshake.auth.device_id as string const deviceId = socket.handshake.auth.device_id as string
const sessionId = socket.handshake.auth.session_id as string
const clientInfo = { const clientInfo = {
userId: '', userId: '',
deviceId, deviceId,
sessionId,
ip, ip,
socket, socket,
} }
@@ -73,7 +78,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`)
delete this.clients[clientInfo.userId][deviceId] delete this.clients[clientInfo.userId][deviceId + '_' + sessionId]
} }
}) })
console.log(chalk.yellow('[連]') + ` ${ip} connected`) console.log(chalk.yellow('[連]') + ` ${ip} connected`)

View File

@@ -1,6 +1,5 @@
import { Buffer } from "node:buffer"; import { Buffer } from "node:buffer"
import Chat from "../data/Chat.ts"; import Chat from "../data/Chat.ts"
import ChatPrivate from "../data/ChatPrivate.ts"
import FileManager from "../data/FileManager.ts" import FileManager from "../data/FileManager.ts"
import MessagesManager from "../data/MessagesManager.ts" import MessagesManager from "../data/MessagesManager.ts"
import User from "../data/User.ts" import User from "../data/User.ts"

View File

@@ -18,7 +18,7 @@ export default class UserApi extends BaseApi {
msg: "參數缺失", msg: "參數缺失",
code: 400, code: 400,
} }
const { deviceId, ip, socket } = clientInfo const { deviceId, ip, socket, sessionId } = clientInfo
try { try {
const access_token = TokenManager.decode(args.access_token as string) const access_token = TokenManager.decode(args.access_token as string)
@@ -38,9 +38,9 @@ export default class UserApi extends BaseApi {
clientInfo.userId = access_token.author clientInfo.userId = access_token.author
console.log(chalk.green('[驗]') + ` ${access_token.author} authed on Client ${deviceId} (ip = ${ip})`) console.log(chalk.green('[驗]') + ` ${access_token.author} authed on Client ${deviceId} (ip = ${ip})`)
if (ApiManager.clients[clientInfo.userId] == null) ApiManager.clients[clientInfo.userId] = { if (ApiManager.clients[clientInfo.userId] == null) ApiManager.clients[clientInfo.userId] = {
[deviceId]: socket [deviceId + '_' + sessionId]: socket
} }
else ApiManager.clients[clientInfo.userId][deviceId] = socket else ApiManager.clients[clientInfo.userId][deviceId + '_' + sessionId] = socket
return { return {
msg: "成功", msg: "成功",

View File

@@ -4,6 +4,7 @@ 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
sessionId: string
ip: string ip: string
socket: SocketIo.Socket<SocketIo.DefaultEventsMap, SocketIo.DefaultEventsMap, SocketIo.DefaultEventsMap, any> socket: SocketIo.Socket<SocketIo.DefaultEventsMap, SocketIo.DefaultEventsMap, SocketIo.DefaultEventsMap, any>
}) => ApiCallbackMessage | Promise<ApiCallbackMessage> }) => ApiCallbackMessage | Promise<ApiCallbackMessage>