mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-07-03 21:50:45 +00:00
try-catch on websocket message to prevent crashing server
This commit is contained in:
+14
-10
@@ -76,17 +76,21 @@ export class GameServer {
|
||||
this.allClients.set(client.clientID, 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(`${this.id}: 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(`${this.id}: 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 handline websocket request in game server: ${error}`)
|
||||
}
|
||||
})
|
||||
client.ws.on('close', () => {
|
||||
|
||||
+36
-32
@@ -107,40 +107,44 @@ 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({
|
||||
logKey: 'websocket_msg',
|
||||
msg: 'server received websocket message',
|
||||
data: clientMsg,
|
||||
severity: 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,
|
||||
clientMsg.persistentID,
|
||||
ip,
|
||||
clientMsg.username,
|
||||
ws
|
||||
),
|
||||
clientMsg.gameID,
|
||||
clientMsg.lastTurn
|
||||
)
|
||||
}
|
||||
if (clientMsg.type == "log") {
|
||||
try {
|
||||
const clientMsg: ClientMessage = ClientMessageSchema.parse(JSON.parse(message))
|
||||
slog({
|
||||
logKey: "client_console_log",
|
||||
msg: clientMsg.log,
|
||||
severity: clientMsg.severity,
|
||||
clientID: clientMsg.clientID,
|
||||
gameID: clientMsg.gameID,
|
||||
persistentID: clientMsg.persistentID,
|
||||
logKey: 'websocket_msg',
|
||||
msg: 'server received websocket message',
|
||||
data: clientMsg,
|
||||
severity: 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,
|
||||
clientMsg.persistentID,
|
||||
ip,
|
||||
clientMsg.username,
|
||||
ws
|
||||
),
|
||||
clientMsg.gameID,
|
||||
clientMsg.lastTurn
|
||||
)
|
||||
}
|
||||
if (clientMsg.type == "log") {
|
||||
slog({
|
||||
logKey: "client_console_log",
|
||||
msg: clientMsg.log,
|
||||
severity: clientMsg.severity,
|
||||
clientID: clientMsg.clientID,
|
||||
gameID: clientMsg.gameID,
|
||||
persistentID: clientMsg.persistentID,
|
||||
})
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(`errror handling websocket message: ${error}`)
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user