2015-05-10 22:47:22 +02:00
|
|
|
/**
|
2017-01-11 16:24:33 +01:00
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
2015-05-10 22:47:22 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
|
|
|
module.exports = function(RED) {
|
|
|
|
"use strict";
|
|
|
|
var ws = require("ws");
|
2015-10-09 14:36:35 +02:00
|
|
|
var inspect = require("util").inspect;
|
2015-05-10 22:47:22 +02:00
|
|
|
|
|
|
|
// A node red node that sets up a local websocket server
|
|
|
|
function WebSocketListenerNode(n) {
|
|
|
|
// Create a RED node
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
var node = this;
|
|
|
|
|
|
|
|
// Store local copies of the node configuration (as defined in the .html)
|
|
|
|
node.path = n.path;
|
|
|
|
node.wholemsg = (n.wholemsg === "true");
|
|
|
|
|
|
|
|
node._inputNodes = []; // collection of nodes that want to receive events
|
|
|
|
node._clients = {};
|
|
|
|
// match absolute url
|
|
|
|
node.isServer = !/^ws{1,2}:\/\//i.test(node.path);
|
|
|
|
node.closing = false;
|
2018-01-25 21:26:35 +01:00
|
|
|
node.tls = n.tls;
|
2015-05-10 22:47:22 +02:00
|
|
|
|
|
|
|
function startconn() { // Connect to remote endpoint
|
2017-01-17 21:48:05 +01:00
|
|
|
node.tout = null;
|
2018-01-25 21:26:35 +01:00
|
|
|
var options = {};
|
|
|
|
if (node.tls) {
|
|
|
|
var tlsNode = RED.nodes.getNode(node.tls);
|
|
|
|
if (tlsNode) {
|
|
|
|
tlsNode.addTLSOptions(options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var socket = new ws(node.path,options);
|
2016-09-24 00:36:58 +02:00
|
|
|
socket.setMaxListeners(0);
|
2015-05-10 22:47:22 +02:00
|
|
|
node.server = socket; // keep for closing
|
|
|
|
handleConnection(socket);
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleConnection(/*socket*/socket) {
|
|
|
|
var id = (1+Math.random()*4294967295).toString(16);
|
|
|
|
if (node.isServer) { node._clients[id] = socket; node.emit('opened',Object.keys(node._clients).length); }
|
|
|
|
socket.on('open',function() {
|
|
|
|
if (!node.isServer) { node.emit('opened',''); }
|
|
|
|
});
|
|
|
|
socket.on('close',function() {
|
2015-10-24 00:01:25 +02:00
|
|
|
if (node.isServer) { delete node._clients[id]; node.emit('closed',Object.keys(node._clients).length); }
|
2015-05-10 22:47:22 +02:00
|
|
|
else { node.emit('closed'); }
|
|
|
|
if (!node.closing && !node.isServer) {
|
2017-01-17 21:48:05 +01:00
|
|
|
clearTimeout(node.tout);
|
2016-10-10 12:08:06 +02:00
|
|
|
node.tout = setTimeout(function() { startconn(); }, 3000); // try to reconnect every 3 secs... bit fast ?
|
2015-05-10 22:47:22 +02:00
|
|
|
}
|
|
|
|
});
|
2016-10-10 12:08:06 +02:00
|
|
|
socket.on('message',function(data,flags) {
|
2015-05-10 22:47:22 +02:00
|
|
|
node.handleEvent(id,socket,'message',data,flags);
|
|
|
|
});
|
|
|
|
socket.on('error', function(err) {
|
|
|
|
node.emit('erro');
|
|
|
|
if (!node.closing && !node.isServer) {
|
2017-01-17 21:48:05 +01:00
|
|
|
clearTimeout(node.tout);
|
2016-10-10 12:08:06 +02:00
|
|
|
node.tout = setTimeout(function() { startconn(); }, 3000); // try to reconnect every 3 secs... bit fast ?
|
2015-05-10 22:47:22 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node.isServer) {
|
|
|
|
var path = RED.settings.httpNodeRoot || "/";
|
|
|
|
path = path + (path.slice(-1) == "/" ? "":"/") + (node.path.charAt(0) == "/" ? node.path.substring(1) : node.path);
|
|
|
|
|
|
|
|
// Workaround https://github.com/einaros/ws/pull/253
|
|
|
|
// Listen for 'newListener' events from RED.server
|
|
|
|
node._serverListeners = {};
|
|
|
|
|
2016-10-10 12:08:06 +02:00
|
|
|
var storeListener = function(/*String*/event,/*function*/listener) {
|
|
|
|
if (event == "error" || event == "upgrade" || event == "listening") {
|
2015-05-10 22:47:22 +02:00
|
|
|
node._serverListeners[event] = listener;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RED.server.addListener('newListener',storeListener);
|
|
|
|
|
2017-01-24 22:37:08 +01:00
|
|
|
var serverOptions = {
|
2016-02-10 22:43:37 +01:00
|
|
|
server:RED.server,
|
2017-01-24 22:37:08 +01:00
|
|
|
path:path
|
|
|
|
}
|
|
|
|
if (RED.settings.webSocketNodeVerifyClient) {
|
|
|
|
serverOptions.verifyClient = RED.settings.webSocketNodeVerifyClient;
|
|
|
|
}
|
|
|
|
// Create a WebSocket Server
|
|
|
|
node.server = new ws.Server(serverOptions);
|
2015-05-10 22:47:22 +02:00
|
|
|
|
|
|
|
// Workaround https://github.com/einaros/ws/pull/253
|
|
|
|
// Stop listening for new listener events
|
|
|
|
RED.server.removeListener('newListener',storeListener);
|
2016-09-24 00:36:58 +02:00
|
|
|
node.server.setMaxListeners(0);
|
2015-05-10 22:47:22 +02:00
|
|
|
node.server.on('connection', handleConnection);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
node.closing = false;
|
|
|
|
startconn(); // start outbound connection
|
|
|
|
}
|
|
|
|
|
|
|
|
node.on("close", function() {
|
|
|
|
// Workaround https://github.com/einaros/ws/pull/253
|
|
|
|
// Remove listeners from RED.server
|
|
|
|
if (node.isServer) {
|
|
|
|
var listener = null;
|
|
|
|
for (var event in node._serverListeners) {
|
|
|
|
if (node._serverListeners.hasOwnProperty(event)) {
|
|
|
|
listener = node._serverListeners[event];
|
|
|
|
if (typeof listener === "function") {
|
|
|
|
RED.server.removeListener(event,listener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
node._serverListeners = {};
|
|
|
|
node.server.close();
|
|
|
|
node._inputNodes = [];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
node.closing = true;
|
|
|
|
node.server.close();
|
2017-01-17 21:48:05 +01:00
|
|
|
if (node.tout) {
|
|
|
|
clearTimeout(node.tout);
|
|
|
|
node.tout = null;
|
|
|
|
}
|
2015-05-10 22:47:22 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("websocket-listener",WebSocketListenerNode);
|
|
|
|
RED.nodes.registerType("websocket-client",WebSocketListenerNode);
|
|
|
|
|
|
|
|
WebSocketListenerNode.prototype.registerInputNode = function(/*Node*/handler) {
|
|
|
|
this._inputNodes.push(handler);
|
|
|
|
}
|
|
|
|
|
2015-10-24 00:01:25 +02:00
|
|
|
WebSocketListenerNode.prototype.removeInputNode = function(/*Node*/handler) {
|
|
|
|
this._inputNodes.forEach(function(node, i, inputNodes) {
|
|
|
|
if (node === handler) {
|
|
|
|
inputNodes.splice(i, 1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-10-10 12:08:06 +02:00
|
|
|
WebSocketListenerNode.prototype.handleEvent = function(id,/*socket*/socket,/*String*/event,/*Object*/data,/*Object*/flags) {
|
2015-05-10 22:47:22 +02:00
|
|
|
var msg;
|
|
|
|
if (this.wholemsg) {
|
|
|
|
try {
|
|
|
|
msg = JSON.parse(data);
|
|
|
|
}
|
|
|
|
catch(err) {
|
|
|
|
msg = { payload:data };
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
msg = {
|
|
|
|
payload:data
|
|
|
|
};
|
|
|
|
}
|
|
|
|
msg._session = {type:"websocket",id:id};
|
|
|
|
for (var i = 0; i < this._inputNodes.length; i++) {
|
|
|
|
this._inputNodes[i].send(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WebSocketListenerNode.prototype.broadcast = function(data) {
|
2016-09-24 00:36:58 +02:00
|
|
|
var i;
|
2015-05-10 22:47:22 +02:00
|
|
|
try {
|
2016-09-24 00:36:58 +02:00
|
|
|
if (this.isServer) {
|
|
|
|
for (i = 0; i < this.server.clients.length; i++) {
|
2015-05-10 22:47:22 +02:00
|
|
|
this.server.clients[i].send(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.server.send(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(e) { // swallow any errors
|
|
|
|
this.warn("ws:"+i+" : "+e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WebSocketListenerNode.prototype.reply = function(id,data) {
|
|
|
|
var session = this._clients[id];
|
|
|
|
if (session) {
|
|
|
|
try {
|
|
|
|
session.send(data);
|
|
|
|
}
|
|
|
|
catch(e) { // swallow any errors
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function WebSocketInNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.server = (n.client)?n.client:n.server;
|
|
|
|
var node = this;
|
|
|
|
this.serverConfig = RED.nodes.getNode(this.server);
|
|
|
|
if (this.serverConfig) {
|
|
|
|
this.serverConfig.registerInputNode(this);
|
|
|
|
// TODO: nls
|
|
|
|
this.serverConfig.on('opened', function(n) { node.status({fill:"green",shape:"dot",text:"connected "+n}); });
|
|
|
|
this.serverConfig.on('erro', function() { node.status({fill:"red",shape:"ring",text:"error"}); });
|
2018-01-25 14:27:47 +01:00
|
|
|
this.serverConfig.on('closed', function(n) {
|
|
|
|
if (n > 0) { node.status({fill:"green",shape:"dot",text:"connected "+n}); }
|
|
|
|
else { node.status({fill:"red",shape:"ring",text:"disconnected"}); }
|
|
|
|
});
|
2015-05-10 22:47:22 +02:00
|
|
|
} else {
|
|
|
|
this.error(RED._("websocket.errors.missing-conf"));
|
|
|
|
}
|
2015-10-24 00:01:25 +02:00
|
|
|
this.on('close', function() {
|
2017-02-13 22:39:31 +01:00
|
|
|
if (node.serverConfig) {
|
|
|
|
node.serverConfig.removeInputNode(node);
|
|
|
|
}
|
2016-10-10 12:08:06 +02:00
|
|
|
node.status({});
|
2015-10-24 00:01:25 +02:00
|
|
|
});
|
2015-05-10 22:47:22 +02:00
|
|
|
}
|
|
|
|
RED.nodes.registerType("websocket in",WebSocketInNode);
|
|
|
|
|
|
|
|
function WebSocketOutNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
var node = this;
|
|
|
|
this.server = (n.client)?n.client:n.server;
|
|
|
|
this.serverConfig = RED.nodes.getNode(this.server);
|
|
|
|
if (!this.serverConfig) {
|
2017-02-13 22:39:31 +01:00
|
|
|
return this.error(RED._("websocket.errors.missing-conf"));
|
2015-05-10 22:47:22 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// TODO: nls
|
|
|
|
this.serverConfig.on('opened', function(n) { node.status({fill:"green",shape:"dot",text:"connected "+n}); });
|
|
|
|
this.serverConfig.on('erro', function() { node.status({fill:"red",shape:"ring",text:"error"}); });
|
2018-01-25 14:27:47 +01:00
|
|
|
this.serverConfig.on('closed', function(n) {
|
|
|
|
if (n > 0) { node.status({fill:"green",shape:"dot",text:"connected "+n}); }
|
|
|
|
else { node.status({fill:"red",shape:"ring",text:"disconnected"}); }
|
|
|
|
});
|
2015-05-10 22:47:22 +02:00
|
|
|
}
|
|
|
|
this.on("input", function(msg) {
|
|
|
|
var payload;
|
|
|
|
if (this.serverConfig.wholemsg) {
|
|
|
|
delete msg._session;
|
|
|
|
payload = JSON.stringify(msg);
|
|
|
|
} else if (msg.hasOwnProperty("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 (payload) {
|
|
|
|
if (msg._session && msg._session.type == "websocket") {
|
|
|
|
node.serverConfig.reply(msg._session.id,payload);
|
|
|
|
} else {
|
2016-10-10 12:08:06 +02:00
|
|
|
node.serverConfig.broadcast(payload,function(error) {
|
2015-05-10 22:47:22 +02:00
|
|
|
if (!!error) {
|
|
|
|
node.warn(RED._("websocket.errors.send-error")+inspect(error));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2016-10-10 12:08:06 +02:00
|
|
|
this.on('close', function() {
|
|
|
|
node.status({});
|
|
|
|
});
|
2015-05-10 22:47:22 +02:00
|
|
|
}
|
|
|
|
RED.nodes.registerType("websocket out",WebSocketOutNode);
|
|
|
|
}
|