mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-22 21:55:22 +00:00
catch validation error message to prevent server from crashing
This commit is contained in:
+14
-10
@@ -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
@@ -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
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user