fix potential crash when users disconnect from client

This commit is contained in:
Andrew Kelley 2014-04-23 17:06:56 -07:00
parent 45b8d28dcb
commit d17157fcc9
3 changed files with 16 additions and 10 deletions

View file

@ -73,10 +73,7 @@ PlayerServer.actions = {
} else {
key = dbFile.key;
}
// client might have disconnected by now
try {
client.sendMessage('importUrl', {id: id, key: key});
} catch (err) {}
client.sendMessage('importUrl', {id: id, key: key});
});
},
},

View file

@ -10,9 +10,10 @@ function ProtocolParser(options) {
this.player = options.player;
this.buffer = "";
this.alreadyClosed = false;
}
ProtocolParser.prototype._read = function(size) {}
ProtocolParser.prototype._read = function(size) {};
ProtocolParser.prototype._write = function(chunk, encoding, callback) {
var self = this;
@ -44,15 +45,18 @@ ProtocolParser.prototype._write = function(chunk, encoding, callback) {
}
self.emit('message', jsonObject.name, jsonObject.args);
}
}
};
ProtocolParser.prototype.sendMessage = function(name, args) {
if (this.alreadyClosed) return;
var jsonObject = {name: name, args: args};
this.push(JSON.stringify(jsonObject));
};
ProtocolParser.prototype.close = function() {
if (this.alreadyClosed) return;
this.push(null);
this.alreadyClosed = true;
};
function extend(o, src) {

View file

@ -11,10 +11,15 @@ function WebSocketApiClient(ws) {
}
WebSocketApiClient.prototype.sendMessage = function(name, args) {
this.ws.send(JSON.stringify({
name: name,
args: args,
}));
try {
this.ws.send(JSON.stringify({
name: name,
args: args,
}));
} catch (err) {
// nothing to do
// client might have disconnected by now
}
};
WebSocketApiClient.prototype.close = function() {