From 50323c3d23f52874b77028ae57057b0664e60555 Mon Sep 17 00:00:00 2001 From: Aziz Khoury Date: Mon, 30 Dec 2013 19:40:57 -0500 Subject: [PATCH] guarding against Errors on Socket Disconnect Saw this ``` TypeError: Cannot call method 'indexOf' of undefined at Socket. (/home/admin/NodeBB/src/websockets.js:108:33) at Socket.EventEmitter.emit [as $emit] (events.js:95:17) at Socket.onDisconnect (/home/admin/NodeBB/node_modules/socket.io/lib/socket.js:153:10) at SocketNamespace.handleDisconnect (/home/admin/NodeBB/node_modules/socket.io/lib/namespace.js:229:46) ``` and this ``` /home/admin/NodeBB/src/websockets.js:113 if (userSockets[uid].length === 0) { ^ TypeError: Cannot read property 'length' of undefined at Socket. (/home/admin/NodeBB/src/websockets.js:113:24) ``` in my logs, when users are disconnecting --- src/websockets.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/websockets.js b/src/websockets.js index 8d16a66c08..310af2fbc9 100644 --- a/src/websockets.js +++ b/src/websockets.js @@ -106,12 +106,12 @@ websockets.init = function(io) { socket.on('disconnect', function() { - var index = userSockets[uid].indexOf(socket); + var index = (userSockets[uid] || []).indexOf(socket); if (index !== -1) { userSockets[uid].splice(index, 1); } - if (userSockets[uid].length === 0) { + if (userSockets[uid] && userSockets[uid].length === 0) { delete users[sessionID]; delete userSockets[uid]; if (uid) {