diff --git a/lib/realtime.js b/lib/realtime.js index 23464099..3e99a12d 100644 --- a/lib/realtime.js +++ b/lib/realtime.js @@ -247,6 +247,15 @@ function emitRefresh(socket) { }); } +function clearSocketQueue(queue, socket) { + for (var i = 0; i < queue.length; i++) { + if (!queue[i] || queue[i].id == socket.id) { + queue.splice(i, 1); + i--; + } + } +} + var isConnectionBusy = false; var connectionSocketQueue = []; var isDisconnectBusy = false; @@ -263,12 +272,7 @@ function finishConnection(socket, note, user) { emitRefresh(socket); //clear finished socket in queue - for (var i = 0; i < connectionSocketQueue.length; i++) { - if (!connectionSocketQueue[i] || connectionSocketQueue[i].id == socket.id) { - connectionSocketQueue.splice(i, 1); - i--; - } - } + clearSocketQueue(connectionSocketQueue, socket); //seek for next socket isConnectionBusy = false; if (connectionSocketQueue.length > 0) @@ -291,6 +295,7 @@ function startConnection(socket) { var notename = getNotenameFromSocket(socket); if (!notename) { + clearSocketQueue(connectionSocketQueue, socket); isConnectionBusy = false; return; } @@ -303,12 +308,7 @@ function startConnection(socket) { }); socket.disconnect(true); //clear err socket in queue - for (var i = 0; i < connectionSocketQueue.length; i++) { - if (!connectionSocketQueue[i] || connectionSocketQueue[i].id == socket.id) { - connectionSocketQueue.splice(i, 1); - i--; - } - } + clearSocketQueue(connectionSocketQueue, socket); isConnectionBusy = false; return logger.error(err); } @@ -320,6 +320,7 @@ function startConnection(socket) { Note.findOrNewNote(notename, permission, function (err, note) { if (err) { responseError(res, "404", "Not Found", "oops."); + clearSocketQueue(connectionSocketQueue, socket); isConnectionBusy = false; return; } @@ -393,12 +394,7 @@ function disconnect(socket) { emitOnlineUsers(socket); //clear finished socket in queue - for (var i = 0; i < disconnectSocketQueue.length; i++) { - if (!disconnectSocketQueue[i] || disconnectSocketQueue[i].id == socket.id) { - disconnectSocketQueue.splice(i, 1); - i--; - } - } + clearSocketQueue(disconnectSocketQueue, socket); //seek for next socket isDisconnectBusy = false; if (disconnectSocketQueue.length > 0)