1
0
forked from noxious/server

Updated several events to new event format

This commit is contained in:
2024-09-22 00:46:17 +02:00
parent 50d13af5d6
commit 81428ea0c2
9 changed files with 298 additions and 203 deletions

View File

@ -1,9 +1,28 @@
import { Server } from 'socket.io'
import { TSocket } from '../utilities/types'
import { gameLogger } from '../utilities/logger'
export default function (io: Server, socket: TSocket) {
socket.on('login', () => {
if (!socket.user) return
socket.emit('logged_in', { user: socket.user })
})
}
export default class LoginEvent {
constructor(
private readonly io: Server,
private readonly socket: TSocket
) {}
public listen(): void {
this.socket.on('login', this.handleLogin.bind(this))
}
private handleLogin(): void {
try {
if (!this.socket.user) {
gameLogger.warn('Login attempt without user data')
return
}
this.socket.emit('logged_in', { user: this.socket.user })
gameLogger.info(`User logged in: ${this.socket.user.id}`)
} catch (error: any) {
gameLogger.error('login error', error.message)
}
}
}