Skip to content

Commit

Permalink
Merge pull request #5 from SoopSASM/feature/add-socket
Browse files Browse the repository at this point in the history
Add simple socket for react and node-red server communication
  • Loading branch information
jekwan authored Sep 21, 2022
2 parents cc8385a + b85057d commit 23e9fb8
Show file tree
Hide file tree
Showing 10 changed files with 545 additions and 33 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
dashboard/dist
40 changes: 35 additions & 5 deletions dashboard/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,55 @@ let initialized = false;
module.exports = function (RED) {
if (!initialized) {
initialized = true;
init(RED.httpNode || RED.httpAdmin, RED.settings);
init(RED);
}

return {
testFunc,
emitState,
};
};

const path = require("path");
const socketio = require("socket.io");
const serveStatic = require("serve-static");

function init(app, settings) {
let io = null;
const dashboardState = {};

function init(RED) {
const app = RED.httpNode || RED.httpAdmin;
const server = RED.server;

io = socketio(server);
initSocket(io);

// const settings = RED.settings;
// const path = `${settings.httpNodeRoot}${settings.path}`;
// app.use(path, serveStatic(path.join(__dirname, "/dist")));

app.use("/dashboard", serveStatic(path.join(__dirname, "/dist")));
console.log("react dashboard started at /dashboard");
}

function testFunc() {
return "test message";
function initSocket(io) {
io.on("connection", (socket) => {
socket.emit("initial-value", dashboardState);
});
}

function emitState(state) {
const node_id = state.node_id;
state.time = Date.now();

if (dashboardState.hasOwnProperty(node_id)) {
dashboardState[node_id].push(state);
} else {
dashboardState[node_id] = [state];
}

emit(state);
}

function emit(state) {
io.emit("update-value", state);
}
10 changes: 10 additions & 0 deletions dashboard/nodes/lower-case.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@
},
],
});

console.log("prepared");

RED.nodes.eachNode((node) => {
console.log(node);
});

RED.nodes.eachConfig((node) => {
console.log(node);
});
},
});
</script>
Expand Down
11 changes: 1 addition & 10 deletions dashboard/nodes/lower-case.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ module.exports = function (RED) {
function LowerCaseNode(config) {
const node = this;
RED.nodes.createNode(node, config);

node.on("input", function (msg, send, done) {
node.log("input has arrived " + dashboard.testFunc());
send =
send ||
function () {
Expand All @@ -19,14 +17,7 @@ module.exports = function (RED) {
if (done) done();
});

node.on("close", function (removed, done) {
if (removed) {
node.log("This node has been disabled/deleted");
} else {
node.log("This node is being restarted");
}
done();
});
node.log("LowerCase Node created");
}

RED.nodes.registerType("lower-case", LowerCaseNode);
Expand Down
4 changes: 0 additions & 4 deletions dashboard/nodes/test-node.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
<script type="text/javascript">
const MY_CATEGORY = "common";
</script>

<script type="text/javascript">
RED.nodes.registerType("test-node", {
category: MY_CATEGORY,
Expand Down
2 changes: 1 addition & 1 deletion dashboard/nodes/test-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = function (RED) {
function TestNode(config) {
const node = this;
RED.nodes.createNode(node, config);
console.log("Test Node created " + dashboard.testFunc());
node.log("Test Node created");
}

RED.nodes.registerType("test-node", TestNode);
Expand Down
24 changes: 20 additions & 4 deletions dashboard/src/components/app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import React from "react";
import React, { useEffect } from "react";

export default function App() {
return <div>Hello Dashboard App</div>;
}
import { initlaizeSocket, disconnectSocket } from "../utils/socket";

const App = () => {
useEffect(() => {
initlaizeSocket();

return () => {
disconnectSocket();
};
}, []);

return (
<>
<div>Hello Dashboard</div>
</>
);
};

export default App;
23 changes: 23 additions & 0 deletions dashboard/src/utils/socket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { io } from "socket.io-client";

let socket;

export const initlaizeSocket = () => {
socket = io();

socket.on("connect", () => {
console.info(`socket connected : ${socket.id}`);
});

socket.on("initial-value", (state) => {
console.log(state);
});

socket.on("update-value", (state) => {
console.log(state);
});
};

export const disconnectSocket = () => {
socket.disconnect();
};
Loading

0 comments on commit 23e9fb8

Please sign in to comment.