fix: 多個同 DeviceId 不同 Session 的客戶端無法同時收到消息
This commit is contained in:
@@ -9,6 +9,7 @@ import randomUUID from "../randomUUID.ts"
|
||||
type UnknownObject = { [key: string]: unknown }
|
||||
|
||||
class Client {
|
||||
static sessionId = randomUUID()
|
||||
static myUserProfile?: User
|
||||
static socket?: Socket
|
||||
static events: { [key: string]: (data: UnknownObject) => UnknownObject | void } = {}
|
||||
@@ -21,7 +22,8 @@ class Client {
|
||||
this.socket = io({
|
||||
transports: ['websocket'],
|
||||
auth: {
|
||||
device_id: data.device_id
|
||||
device_id: data.device_id,
|
||||
session_id: this.sessionId,
|
||||
},
|
||||
})
|
||||
this.socket!.on("connect", async () => {
|
||||
|
||||
@@ -48,6 +48,9 @@ export default class ApiManager {
|
||||
static checkUserIsOnline(userId: string) {
|
||||
return this.getUserClientSockets(userId) != null
|
||||
}
|
||||
/**
|
||||
* 獲取用戶所有的客戶端表 (格式遵循 設備ID_當前Session)
|
||||
*/
|
||||
static getUserClientSockets(userId: string) {
|
||||
return this.clients[userId]
|
||||
}
|
||||
@@ -60,10 +63,12 @@ export default class ApiManager {
|
||||
const ip = socket.conn.remoteAddress
|
||||
|
||||
const deviceId = socket.handshake.auth.device_id as string
|
||||
const sessionId = socket.handshake.auth.session_id as string
|
||||
|
||||
const clientInfo = {
|
||||
userId: '',
|
||||
deviceId,
|
||||
sessionId,
|
||||
ip,
|
||||
socket,
|
||||
}
|
||||
@@ -73,7 +78,7 @@ export default class ApiManager {
|
||||
console.log(chalk.yellow('[斷]') + ` ${ip} disconnected`)
|
||||
else {
|
||||
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`)
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { Buffer } from "node:buffer";
|
||||
import Chat from "../data/Chat.ts";
|
||||
import ChatPrivate from "../data/ChatPrivate.ts"
|
||||
import { Buffer } from "node:buffer"
|
||||
import Chat from "../data/Chat.ts"
|
||||
import FileManager from "../data/FileManager.ts"
|
||||
import MessagesManager from "../data/MessagesManager.ts"
|
||||
import User from "../data/User.ts"
|
||||
|
||||
@@ -18,7 +18,7 @@ export default class UserApi extends BaseApi {
|
||||
msg: "參數缺失",
|
||||
code: 400,
|
||||
}
|
||||
const { deviceId, ip, socket } = clientInfo
|
||||
const { deviceId, ip, socket, sessionId } = clientInfo
|
||||
try {
|
||||
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
|
||||
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
|
||||
[deviceId + '_' + sessionId]: socket
|
||||
}
|
||||
else ApiManager.clients[clientInfo.userId][deviceId] = socket
|
||||
else ApiManager.clients[clientInfo.userId][deviceId + '_' + sessionId] = socket
|
||||
|
||||
return {
|
||||
msg: "成功",
|
||||
|
||||
@@ -4,6 +4,7 @@ import * as SocketIo from "socket.io"
|
||||
type EventCallbackFunction = (args: { [key: string]: unknown }, clientInfo: {
|
||||
userId: string
|
||||
deviceId: string
|
||||
sessionId: string
|
||||
ip: string
|
||||
socket: SocketIo.Socket<SocketIo.DefaultEventsMap, SocketIo.DefaultEventsMap, SocketIo.DefaultEventsMap, any>
|
||||
}) => ApiCallbackMessage | Promise<ApiCallbackMessage>
|
||||
|
||||
Reference in New Issue
Block a user