Skip to content

Commit

Permalink
Merge pull request #11 from SoopSASM/feature/receive-message-from-fe
Browse files Browse the repository at this point in the history
Implement recieve message feature
  • Loading branch information
jekwan authored Sep 26, 2022
2 parents c380045 + 6fa8b51 commit 197c104
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions dashboard/common/common.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const FRONT_SOCKET_TYPE = {
INIT_STATES: "initialize-state",
UPDATE_STATE: "update-state",
RECEIVE_MESSAGE: "receive-message",
};

const EDITOR_SOCKET_TYPE = {
Expand Down
14 changes: 14 additions & 0 deletions dashboard/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = function (RED) {

return {
emitState,
addNode,
};
};

Expand All @@ -18,6 +19,7 @@ const { FRONT_SOCKET_TYPE, EDITOR_SOCKET_TYPE, DASHBOARD_PATH } = require("./com

let io = null;
let dashboardState = {};
let dashboardNodes = {};

function init(RED) {
const app = RED.httpNode || RED.httpAdmin;
Expand All @@ -33,6 +35,14 @@ function initSocket(io) {
io.on("connection", (socket) => {
socket.emit(FRONT_SOCKET_TYPE.INIT_STATES, dashboardState);

socket.on(FRONT_SOCKET_TYPE.RECEIVE_MESSAGE, (message) => {
const node = dashboardNodes[message.nodeId];
if (!node) return;
if (typeof node.onMessage === "function") {
node.onMessage(message);
}
});

socket.on(EDITOR_SOCKET_TYPE.FLOW_DEPLOYED, (nodes) => {
initializeDashboardState(nodes);
io.emit(FRONT_SOCKET_TYPE.INIT_STATES, dashboardState);
Expand Down Expand Up @@ -64,3 +74,7 @@ function emit(state) {
if (Array.isArray(state)) io.emit(FRONT_SOCKET_TYPE.UPDATE_STATE, state);
else io.emit(FRONT_SOCKET_TYPE.UPDATE_STATE, [state]);
}

function addNode(node) {
dashboardNodes[node.id] = node;
}
9 changes: 9 additions & 0 deletions dashboard/nodes/soop_lower_case.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ module.exports = function (RED) {
function LowerCaseNode(config) {
const node = this;
RED.nodes.createNode(node, config);

dashboard.addNode({
node: node,
onMessage: (message) => {
console.log(message);
node.send("received message from a dashboard");
},
});

node.on("input", function (msg, send, done) {
send =
send ||
Expand Down
7 changes: 7 additions & 0 deletions dashboard/src/utils/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,10 @@ export const initlaizeSocket = () => {
export const disconnectSocket = () => {
socket.disconnect();
};

export const sendMessage = (nodeId, messageBody) => {
socket.emit(FRONT_SOCKET_TYPE.RECEIVE_MESSAGE, {
nodeId: nodeId,
...messageBody,
});
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
],
"scripts": {
"start": "webpack-dev-server --mode development --open --hot",
"build": "webpack --mode production"
"builddev": "webpack --mode development",
"buildprod": "webpack --mode production"
},
"license": "Apache 2.0",
"repository": {
Expand Down

0 comments on commit 197c104

Please sign in to comment.