This commit is contained in:
SrGooglo 2025-02-25 23:10:13 +00:00
parent 52dd4ecffd
commit fa83c55264

View File

@ -1,5 +1,4 @@
import buildFunctionHandler from "@utils/buildFunctionHandler" import buildFunctionHandler from "@utils/buildFunctionHandler"
import { Snowflake } from "nodejs-snowflake"
import { ChatMessage } from "@db_models" import { ChatMessage } from "@db_models"
@ -34,7 +33,10 @@ export default class Room {
throw new OperationError(500, "Not implemented") throw new OperationError(500, "Not implemented")
}, },
"room:send:message": async (client, payload) => { "room:send:message": async (client, payload) => {
console.log(`[${this.roomID}] [@${client.userData.username}] sent message >`, payload) console.log(
`[${this.roomID}] [@${client.userData.username}] sent message >`,
payload,
)
let { message } = payload let { message } = payload
@ -43,7 +45,10 @@ export default class Room {
} }
if (message.length > this.limitations.maxMessageLength) { if (message.length > this.limitations.maxMessageLength) {
message = message.substring(0, this.limitations.maxMessageLength) message = message.substring(
0,
this.limitations.maxMessageLength,
)
} }
const created_at = new Date().getTime() const created_at = new Date().getTime()
@ -84,22 +89,26 @@ export default class Room {
} }
default: default:
break; break
}
} }
} }
} }
},
} }
handlers = { handlers = {
join: (client) => { join: (client) => {
if (client.connectedRoomID) { if (client.connectedRoomID) {
console.warn(`[${client.id}][@${client.userData.username}] already connected to room ${client.connectedRoomID}`) console.warn(
`[${client.id}][@${client.userData.username}] already connected to room ${client.connectedRoomID}`,
)
client.leave(client.connectedRoomID) client.leave(client.connectedRoomID)
} }
console.log(`[${client.id}][@${client.userData.username}] joined room ${this.roomID}`) console.log(
`[${client.id}][@${client.userData.username}] joined room ${this.roomID}`,
)
client.connectedRoomID = this.roomID client.connectedRoomID = this.roomID
@ -131,17 +140,21 @@ export default class Room {
username: client.userData.username, username: client.userData.username,
fullName: client.userData.fullName, fullName: client.userData.fullName,
avatar: client.userData.avatar, avatar: client.userData.avatar,
} },
}) })
}, },
leave: (client) => { leave: (client) => {
if (!client.connectedRoomID) { if (!client.connectedRoomID) {
console.warn(`[${client.id}][@${client.userData.username}] not connected to any room`) console.warn(
`[${client.id}][@${client.userData.username}] not connected to any room`,
)
return return
} }
if (client.connectedRoomID !== this.roomID) { if (client.connectedRoomID !== this.roomID) {
console.warn(`[${client.id}][@${client.userData.username}] not connected to room ${this.roomID}, cannot leave`) console.warn(
`[${client.id}][@${client.userData.username}] not connected to room ${this.roomID}, cannot leave`,
)
return false return false
} }
@ -157,20 +170,22 @@ export default class Room {
username: client.userData.username, username: client.userData.username,
fullName: client.userData.fullName, fullName: client.userData.fullName,
avatar: client.userData.avatar, avatar: client.userData.avatar,
} },
}) })
for (const [event, handler] of client.handlers) { for (const [event, handler] of client.handlers) {
client.off(event, handler) client.off(event, handler)
} }
console.log(`[${client.id}][@${client.userData.username}] left room ${this.roomID}`) console.log(
`[${client.id}][@${client.userData.username}] left room ${this.roomID}`,
)
}, },
broadcastToMembers: (event, payload) => { broadcastToMembers: (event, payload) => {
for (const client of this.connections) { for (const client of this.connections) {
client.emit(event, payload) client.emit(event, payload)
} }
} },
} }
getConnectedUsers = () => { getConnectedUsers = () => {