From d5aa00b75386cdfb475b42371e129434417d81ea Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Sun, 21 Oct 2018 11:05:05 +0300 Subject: [PATCH] Fix timing issue crashing on undefined when setting openChannel --- src/client.js | 9 +++++++-- src/server.js | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/client.js b/src/client.js index 8a2a009e..f88be03b 100644 --- a/src/client.js +++ b/src/client.js @@ -404,9 +404,14 @@ Client.prototype.more = function(data) { }; Client.prototype.open = function(socketId, target) { + // Due to how socket.io works internally, normal events may arrive later than + // the disconnect event, and because we can't control this timing precisely, + // process this event normally even if there is no attached client anymore. + const attachedClient = this.attachedClients[socketId] || {}; + // Opening a window like settings if (target === null) { - this.attachedClients[socketId].openChannel = -1; + attachedClient.openChannel = -1; return; } @@ -420,7 +425,7 @@ Client.prototype.open = function(socketId, target) { target.chan.unread = 0; target.chan.highlight = 0; - this.attachedClients[socketId].openChannel = target.chan.id; + attachedClient.openChannel = target.chan.id; this.lastActiveChannel = target.chan.id; this.emit("open", target.chan.id); diff --git a/src/server.js b/src/server.js index 24afd99e..118e437f 100644 --- a/src/server.js +++ b/src/server.js @@ -295,7 +295,7 @@ function initializeClient(socket, client, token, lastMessage) { } socket.on("disconnect", function() { - client.clientDetach(socket.id); + process.nextTick(() => client.clientDetach(socket.id)); }); socket.on("input", (data) => {