Fixed user might not disconnect from server properly, handle more uncaught exceptions

This commit is contained in:
Wu Cheng-Han 2015-09-24 11:40:08 +08:00
parent 43a175e710
commit f1a0dff9e6
2 changed files with 16 additions and 6 deletions

View File

@ -51,7 +51,7 @@ EditorSocketIOServer.prototype.addClient = function (socket) {
try { try {
self.onOperation(socket, revision, operation, selection); self.onOperation(socket, revision, operation, selection);
} catch (err) { } catch (err) {
socket.disconnect(); socket.disconnect(true);
} }
}); });
}); });

View File

@ -171,9 +171,12 @@ function getStatus(callback) {
} }
function getNotenameFromSocket(socket) { function getNotenameFromSocket(socket) {
if (!socket || !socket.handshake || !socket.handshake.headers) {
return;
}
var referer = socket.handshake.headers.referer; var referer = socket.handshake.headers.referer;
if (!referer) { if (!referer) {
return socket.disconnect(); return socket.disconnect(true);
} }
var hostUrl = url.parse(referer); var hostUrl = url.parse(referer);
var notename = hostUrl.pathname.split('/')[1]; var notename = hostUrl.pathname.split('/')[1];
@ -184,7 +187,7 @@ function getNotenameFromSocket(socket) {
socket.emit('info', { socket.emit('info', {
code: 404 code: 404
}); });
return socket.disconnect(); return socket.disconnect(true);
} }
notename = LZString.decompressFromBase64(notename); notename = LZString.decompressFromBase64(notename);
return notename; return notename;
@ -281,7 +284,10 @@ function startConnection(socket) {
isConnectionBusy = true; isConnectionBusy = true;
var notename = getNotenameFromSocket(socket); var notename = getNotenameFromSocket(socket);
if (!notename) return; if (!notename) {
isConnectionBusy = false;
return;
}
if (!notes[notename]) { if (!notes[notename]) {
db.readFromDB(notename, function (err, data) { db.readFromDB(notename, function (err, data) {
@ -289,7 +295,7 @@ function startConnection(socket) {
socket.emit('info', { socket.emit('info', {
code: 404 code: 404
}); });
socket.disconnect(); socket.disconnect(true);
//clear err socket in queue //clear err socket in queue
for (var i = 0; i < connectionSocketQueue.length; i++) { for (var i = 0; i < connectionSocketQueue.length; i++) {
if (connectionSocketQueue[i].id == socket.id) if (connectionSocketQueue[i].id == socket.id)
@ -306,6 +312,7 @@ function startConnection(socket) {
Note.findOrNewNote(notename, permission, function (err, note) { Note.findOrNewNote(notename, permission, function (err, note) {
if (err) { if (err) {
responseError(res, "404", "Not Found", "oops."); responseError(res, "404", "Not Found", "oops.");
isConnectionBusy = false;
return; return;
} }
var body = LZString.decompressFromBase64(data.rows[0].content); var body = LZString.decompressFromBase64(data.rows[0].content);
@ -338,7 +345,10 @@ function disconnect(socket) {
logger.info(JSON.stringify(users[socket.id])); logger.info(JSON.stringify(users[socket.id]));
} }
var notename = getNotenameFromSocket(socket); var notename = getNotenameFromSocket(socket);
if (!notename) return; if (!notename) {
isDisconnectBusy = false;
return;
}
if (users[socket.id]) { if (users[socket.id]) {
delete users[socket.id]; delete users[socket.id];
} }