From ba3a4ceb1dbf0cdf6a6d1b4d304aaa59f0c4bd7b Mon Sep 17 00:00:00 2001 From: javis86 Date: Mon, 23 Aug 2021 19:45:02 -0300 Subject: [PATCH 1/2] Added librae8226/node-red-contrib-socketio solution for node-red instance hangs when is binded to node-red --- README.md | 9 +++++++-- package.json | 10 +++++----- socketio.js | 11 ++++++++++- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 2e5b9a0..4cc2e05 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,12 @@ -# node-red-contrib-socketio +# node-red-contrib-socketio-javis86 Implementation for [Node-RED](https://nodered.org/) of the popular [Socket.IO](http://socket.io/). +from original project node-red-contrib-socketio + ## Installation To install node-red-contrib-socketio use this command -`npm i node-red-contrib-socketio` +`npm i node-red-contrib-socketio-javis86` ## Composition The Socket.IO implementation is made with @@ -26,3 +28,6 @@ Thank to: * @bimalyn-IBM for implementig rooms * @essuraj for implementig rooms listing node * @cazellap for pushong adding compatibility to socketIO 3.0 +* @javis86 individual contributor for fixing node-red hangs when deploy flows + + diff --git a/package.json b/package.json index a97907b..726ae28 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "node-red-contrib-socketio", - "version": "1.1.0", + "name": "node-red-contrib-socketio-javis86", + "version": "1.1.1", "description": "Implementation for Node-RED of a SocketIO Sever", "dependencies": { "socket.io": ">=3.1.0" @@ -16,7 +16,7 @@ }, "repository": { "type": "git", - "url": "git+https://github.com/wperw/node-red-contrib-socketio.git" + "url": "git+https://github.com/javis86/node-red-contrib-socketio.git" }, "keywords": [ "node-red" @@ -24,7 +24,7 @@ "author": "Telemaco Gallimberti", "license": "MIT", "bugs": { - "url": "https://github.com/wperw/node-red-contrib-socketio/issues" + "url": "https://github.com/javis86/node-red-contrib-socketio/issues" }, - "homepage": "https://github.com/wperw/node-red-contrib-socketio#readme" + "homepage": "https://github.com/javis86/node-red-contrib-socketio#readme" } diff --git a/socketio.js b/socketio.js index 1ee6319..0e71d23 100644 --- a/socketio.js +++ b/socketio.js @@ -6,6 +6,7 @@ module.exports = function(RED) { const { Server } = require("socket.io"); var io; var customProperties = {}; + var sockets = []; function socketIoConfig(n) { RED.nodes.createNode(this, n); @@ -29,7 +30,14 @@ module.exports = function(RED) { node.log("Created server " + bindOn); node.on("close", function() { - io.close(); + if (!this.bindToNode) { + io.close(); + } + sockets.forEach(function (socket) { + node.log('disconnect:' + socket.id); + socket.disconnect(true); + }); + sockets = []; }); } @@ -80,6 +88,7 @@ module.exports = function(RED) { } io.on("connection", function(socket) { + sockets.push(socket); node.rules.forEach(function(val, i) { addListener(socket, val, i); }); From 4fd5560124c92b889c104e31d726606233a02ef7 Mon Sep 17 00:00:00 2001 From: javis86 Date: Fri, 27 Aug 2021 10:39:51 -0300 Subject: [PATCH 2/2] CORS config added --- package.json | 10 +-- socketio.html | 238 ++++++++++++++++++++++++++------------------------ socketio.js | 29 ++++-- 3 files changed, 155 insertions(+), 122 deletions(-) diff --git a/package.json b/package.json index 726ae28..a97907b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "node-red-contrib-socketio-javis86", - "version": "1.1.1", + "name": "node-red-contrib-socketio", + "version": "1.1.0", "description": "Implementation for Node-RED of a SocketIO Sever", "dependencies": { "socket.io": ">=3.1.0" @@ -16,7 +16,7 @@ }, "repository": { "type": "git", - "url": "git+https://github.com/javis86/node-red-contrib-socketio.git" + "url": "git+https://github.com/wperw/node-red-contrib-socketio.git" }, "keywords": [ "node-red" @@ -24,7 +24,7 @@ "author": "Telemaco Gallimberti", "license": "MIT", "bugs": { - "url": "https://github.com/javis86/node-red-contrib-socketio/issues" + "url": "https://github.com/wperw/node-red-contrib-socketio/issues" }, - "homepage": "https://github.com/javis86/node-red-contrib-socketio#readme" + "homepage": "https://github.com/wperw/node-red-contrib-socketio#readme" } diff --git a/socketio.html b/socketio.html index 59dbd20..4e484b6 100644 --- a/socketio.html +++ b/socketio.html @@ -1,32 +1,35 @@ @@ -34,10 +37,10 @@
-
+
-
+
@@ -47,7 +50,18 @@
- +
+ + +
+
+ + +
+
+ + +
@@ -139,19 +153,19 @@ @@ -187,19 +201,19 @@ @@ -226,19 +240,19 @@ @@ -264,19 +278,19 @@ @@ -297,4 +311,4 @@ + \ No newline at end of file diff --git a/socketio.js b/socketio.js index 0e71d23..355e51b 100644 --- a/socketio.js +++ b/socketio.js @@ -14,12 +14,31 @@ module.exports = function(RED) { this.port = n.port || 80; this.sendClient = n.sendClient; this.path = n.path || "/socket.io"; - this.bindToNode = n.bindToNode || false; + this.bindToNode = n.bindToNode || false; + this.corsOrigins = n.corsOrigins || "*"; + this.corsMethods = n.corsMethods.toUpperCase().split(",") || "GET,POST"; + this.enableCors = n.enableCors || false; + + node.log("socketIoConfig - CORS METHODS " + JSON.stringify(this.corsMethods)); + node.log("socketIoConfig - CORS ORIGINS " + JSON.stringify(this.corsOrigins)); + node.log("socketIoConfig - CORS METHODS " + JSON.stringify(this.enableCors)); + + let corsOptions = {}; + + if (this.enableCors) { + corsOptions = { + cors: { + origin: this.corsOrigins, + methods: this.corsMethods + } + }; + } - if (this.bindToNode) { - io = new Server(RED.server); - } else { - io = new Server(); + if (this.bindToNode) { + io = new Server(RED.server, corsOptions); + } else { + io = new Server(corsOptions); + io.serveClient(node.sendClient); io.path(node.path); io.listen(node.port);