Skip to content

Commit 1cae0c5

Browse files
committed
Update to prevent duplicate socket push in queue in order to lower down server loading
1 parent d23ced1 commit 1cae0c5

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

lib/realtime.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,15 @@ function emitRefresh(socket) {
327327
socket.emit('refresh', out);
328328
}
329329

330+
function isDuplicatedInSocketQueue(queue, socket) {
331+
for (var i = 0; i < queue.length; i++) {
332+
if (queue[i] && queue[i].id == socket.id) {
333+
return true;
334+
}
335+
}
336+
return false;
337+
}
338+
330339
function clearSocketQueue(queue, socket) {
331340
for (var i = 0; i < queue.length; i++) {
332341
if (!queue[i] || queue[i].id == socket.id) {
@@ -654,6 +663,8 @@ function connection(socket) {
654663
if (!noteId) {
655664
return failConnection(404, 'note id not found', socket);
656665
}
666+
667+
if (isDuplicatedInSocketQueue(socket, connectionSocketQueue)) return;
657668

658669
// store noteId in this socket session
659670
socket.noteId = noteId;
@@ -830,6 +841,7 @@ function connection(socket) {
830841

831842
//when a new client disconnect
832843
socket.on('disconnect', function () {
844+
if (isDuplicatedInSocketQueue(socket, disconnectSocketQueue)) return;
833845
disconnectSocketQueue.push(socket);
834846
disconnect(socket);
835847
});

0 commit comments

Comments
 (0)