1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Add try/catch to websocket to fix #402

Also allow buffer out as binary if required. (ie don't try and stringify a buffer)
This commit is contained in:
Dave C-J 2014-09-18 17:10:01 +01:00
parent a718f34c58
commit ed1da5cf7b

View File

@ -59,7 +59,6 @@ module.exports = function(RED) {
node.server.on('connection', function(socket){ node.server.on('connection', function(socket){
var id = (1+Math.random()*4294967295).toString(16); var id = (1+Math.random()*4294967295).toString(16);
node._clients[id] = socket; node._clients[id] = socket;
socket.on('close',function() { socket.on('close',function() {
delete node._clients[id]; delete node._clients[id];
}); });
@ -84,7 +83,6 @@ module.exports = function(RED) {
} }
} }
node._serverListeners = {}; node._serverListeners = {};
node.server.close(); node.server.close();
node._inputNodes = []; node._inputNodes = [];
}); });
@ -117,8 +115,13 @@ module.exports = function(RED) {
} }
WebSocketListenerNode.prototype.broadcast = function(data){ WebSocketListenerNode.prototype.broadcast = function(data){
for (var i = 0; i < this.server.clients.length; i++) { try {
this.server.clients[i].send(data); for (var i = 0; i < this.server.clients.length; i++) {
this.server.clients[i].send(data);
}
}
catch(e) { // swallow any errors
this.warn("ws:"+i+" : "+e);
} }
} }
@ -160,7 +163,12 @@ module.exports = function(RED) {
delete msg._session; delete msg._session;
payload = JSON.stringify(msg); payload = JSON.stringify(msg);
} else { } else {
payload = RED.util.ensureString(msg.payload); if (!Buffer.isBuffer(msg.payload)) { // if it's not a buffer make sure it's a string.
payload = RED.util.ensureString(msg.payload);
}
else {
payload = msg.payload;
}
} }
if (msg._session && msg._session.type == "websocket") { if (msg._session && msg._session.type == "websocket") {
node.serverConfig.send(msg._session.id,payload); node.serverConfig.send(msg._session.id,payload);