catch validation error message to prevent server from crashing

This commit is contained in:
Evan
2024-12-23 11:24:13 -08:00
parent 70fefe347b
commit 9ef0898676
2 changed files with 26 additions and 19 deletions
+14 -10
View File
@@ -70,17 +70,21 @@ export class GameServer {
this.allClients.set(client.id, client)
client.ws.on('message', (message: string) => {
const clientMsg: ClientMessage = ClientMessageSchema.parse(JSON.parse(message))
if (clientMsg.type == "intent") {
if (clientMsg.gameID == this.id) {
this.addIntent(clientMsg.intent)
} else {
console.warn(`client ${clientMsg.clientID} sent to wrong game`)
try {
const clientMsg: ClientMessage = ClientMessageSchema.parse(JSON.parse(message))
if (clientMsg.type == "intent") {
if (clientMsg.gameID == this.id) {
this.addIntent(clientMsg.intent)
} else {
console.warn(`client ${clientMsg.clientID} sent to wrong game`)
}
}
}
if (clientMsg.type == "ping") {
this.lastPingUpdate = Date.now()
client.lastPing = Date.now()
if (clientMsg.type == "ping") {
this.lastPingUpdate = Date.now()
client.lastPing = Date.now()
}
} catch (error) {
console.log(`error handling message in game server: ${error}`)
}
})
client.ws.on('close', () => {
+12 -9
View File
@@ -94,17 +94,20 @@ app.get('/private_lobby/:id', (req, res) => {
wss.on('connection', (ws, req) => {
ws.on('message', (message: string) => {
const clientMsg: ClientMessage = ClientMessageSchema.parse(JSON.parse(message))
slog('websocket_msg', 'server received websocket message', clientMsg, LogSeverity.DEBUG)
if (clientMsg.type == "join") {
const forwarded = req.headers['x-forwarded-for']
const ip = Array.isArray(forwarded)
? forwarded[0] // Get the first IP if it's an array
: forwarded || req.socket.remoteAddress;
try {
const clientMsg: ClientMessage = ClientMessageSchema.parse(JSON.parse(message))
slog('websocket_msg', 'server received websocket message', clientMsg, LogSeverity.DEBUG)
if (clientMsg.type == "join") {
const forwarded = req.headers['x-forwarded-for']
const ip = Array.isArray(forwarded)
? forwarded[0] // Get the first IP if it's an array
: forwarded || req.socket.remoteAddress;
gm.addClient(new Client(clientMsg.clientID, ip, ws), clientMsg.gameID, clientMsg.lastTurn)
gm.addClient(new Client(clientMsg.clientID, ip, ws), clientMsg.gameID, clientMsg.lastTurn)
}
} catch (error) {
console.log(`error handling websocket connection: ${error}`)
}
// TODO: send error message
})
});