summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/realtime.js27
1 files changed, 18 insertions, 9 deletions
diff --git a/lib/realtime.js b/lib/realtime.js
index 8ff857e1..0e9af740 100644
--- a/lib/realtime.js
+++ b/lib/realtime.js
@@ -80,6 +80,8 @@ var updater = setInterval(function () {
if (note.server.isDirty) {
if (config.debug) logger.info("updater found dirty note: " + key);
updateNote(note, function(err, _note) {
+ // handle when note already been clean up
+ if (!notes[key] || !notes[key].server) return callback(null, null);
if (!_note) {
realtime.io.to(note.id).emit('info', {
code: 404
@@ -89,7 +91,9 @@ var updater = setInterval(function () {
if (err || !_note) {
for (var i = 0, l = note.socks.length; i < l; i++) {
var sock = note.socks[i];
- sock.disconnect(true);
+ if (typeof sock !== 'undefined' && sock) {
+ sock.disconnect(true);
+ }
}
return callback(err, null);
}
@@ -139,6 +143,7 @@ function updateNote(note, callback) {
});
}
function finishUpdateNote(note, _note, callback) {
+ if (!note || !note.server) return callback(null, null);
var body = note.server.document;
var title = models.Note.parseNoteTitle(body);
title = LZString.compressToBase64(title);
@@ -329,6 +334,16 @@ function connectNextSocket() {
startConnection(connectionSocketQueue[0]);
}
+function interruptConnection(socket, note, user) {
+ if (note) delete note;
+ if (user) delete user;
+ if (socket)
+ clearSocketQueue(connectionSocketQueue, socket);
+ else
+ connectionSocketQueue.shift();
+ connectNextSocket();
+}
+
var isConnectionBusy = false;
var connectionSocketQueue = [];
var isDisconnectBusy = false;
@@ -337,20 +352,14 @@ var disconnectSocketQueue = [];
function finishConnection(socket, note, user) {
// if no valid info provided will drop the client
if (!socket || !note || !user) {
- if (note) delete note;
- if (user) delete user;
- if (socket)
- clearSocketQueue(connectionSocketQueue, socket);
- else
- connectionSocketQueue.shift();
- connectNextSocket();
- return;
+ return interruptConnection(socket, note, user);
}
//check view permission
if (note.permission == 'private') {
if (socket.request.user && socket.request.user.logged_in && socket.request.user.id == note.owner) {
//na
} else {
+ interruptConnection(socket, note, user);
return failConnection(403, 'connection forbidden', socket);
}
}