mirror of
				https://github.com/node-red/node-red.git
				synced 2025-03-01 10:36:34 +00: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:
		@@ -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,10 +115,15 @@ module.exports = function(RED) {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    WebSocketListenerNode.prototype.broadcast = function(data){
 | 
					    WebSocketListenerNode.prototype.broadcast = function(data){
 | 
				
			||||||
 | 
					        try {
 | 
				
			||||||
            for (var i = 0; i < this.server.clients.length; i++) {
 | 
					            for (var i = 0; i < this.server.clients.length; i++) {
 | 
				
			||||||
                this.server.clients[i].send(data);
 | 
					                this.server.clients[i].send(data);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					        catch(e) { // swallow any errors
 | 
				
			||||||
 | 
					            this.warn("ws:"+i+" : "+e);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    WebSocketListenerNode.prototype.send = function(id,data) {
 | 
					    WebSocketListenerNode.prototype.send = function(id,data) {
 | 
				
			||||||
        var session = this._clients[id];
 | 
					        var session = this._clients[id];
 | 
				
			||||||
@@ -160,8 +163,13 @@ module.exports = function(RED) {
 | 
				
			|||||||
                delete msg._session;
 | 
					                delete msg._session;
 | 
				
			||||||
                payload = JSON.stringify(msg);
 | 
					                payload = JSON.stringify(msg);
 | 
				
			||||||
            } else {
 | 
					            } else {
 | 
				
			||||||
 | 
					                if (!Buffer.isBuffer(msg.payload)) { // if it's not a buffer make sure it's a string.
 | 
				
			||||||
                    payload = RED.util.ensureString(msg.payload);
 | 
					                    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);
 | 
				
			||||||
            } else {
 | 
					            } else {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user