From 841d03cdec974dfe9fe9c2119463340406753e17 Mon Sep 17 00:00:00 2001 From: Sam Date: Sat, 21 Jul 2018 02:46:54 +1000 Subject: [PATCH] Server error handling --- server.js | 46 +++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/server.js b/server.js index ffe679c6..577045d5 100644 --- a/server.js +++ b/server.js @@ -220,26 +220,34 @@ ptserver_io.on('connection', (socket) => { socket.disconnect(disconnect) } function updateUserData (username, userData, room) { - for (var i in ptserver_io.sockets.adapter.rooms[room].users) { - var user = ptserver_io.sockets.adapter.rooms[room].users[i] - if (user.username === username) { - // This is our user - user.time = userData.time - user.maxTime = userData.maxTime - user.title = userData.title - user.lastHeartbeat = (new Date()).getTime() - user.playerState = userData.playerState - user.rawTitle = userData.rawTitle - user.clientResponseTime = userData.clientResponseTime - user.type = userData.type - user.showName = userData.showName || '' - user.playerProduct = userData.playerProduct || '' - user.status = userData.status || 'unknown' - user.machineIdentifier = userData.machineIdentifier || '' - user.key = userData.key - user.uuid = userData.uuid - return + if (!room === undefined || room === undefined || room === null) { + console.log('Tried to update a user who isnt in a room', username, userData, room) + return false + } + try { + for (var i in ptserver_io.sockets.adapter.rooms[room].users) { + var user = ptserver_io.sockets.adapter.rooms[room].users[i] + if (user.username === username) { + // This is our user + user.time = userData.time + user.maxTime = userData.maxTime + user.title = userData.title + user.lastHeartbeat = (new Date()).getTime() + user.playerState = userData.playerState + user.rawTitle = userData.rawTitle + user.clientResponseTime = userData.clientResponseTime + user.type = userData.type + user.showName = userData.showName || '' + user.playerProduct = userData.playerProduct || '' + user.status = userData.status || 'unknown' + user.machineIdentifier = userData.machineIdentifier || '' + user.key = userData.key + user.uuid = userData.uuid + return + } } + } catch (e) { + console.log('Failed to update a user', username, userData, room) } } function transferHost (user, newHostPredicate) {