Files
hpcore/examples/nodejs_contract/echo_contract.js
Chalith Desaman 5f40aebf08 NPL message refactor. (#132)
* Implemented feeding and broadcasting npl messages from the contract execution in real-time.
* Replaced npl pipe with domain sockets.
* Refactored npl read and write in nodejs echo contract
2020-10-14 15:18:00 +05:30

53 lines
1.4 KiB
JavaScript

const { HotPocketContract } = require("./hp-contract-lib");
const fs = require('fs');
const hpc = new HotPocketContract();
//console.log("===Echo contract started===");
// We just save execution timestamp as an example state file change.
if (!hpc.readonly)
fs.appendFileSync("exects.txt", "ts:" + hpc.timestamp + "\n");
Object.keys(hpc.users).forEach(async (key) => {
const user = hpc.users[key];
const inputBuf = await user.readInput();
if (inputBuf) {
const userInput = inputBuf.toString("utf8");
if (userInput == "ts")
user.sendOutput(fs.readFileSync("exects.txt"));
else
user.sendOutput("Echoing: " + userInput);
}
});
const npl = hpc.npl;
// Npl channel always connected if contract is not in readonly mode.
// Smart contract developer has to mannually close the channel once the execution logic is complete.
if (npl) {
npl.closeNplChannel();
}
// Npl message sending and receiving template.
// if (npl) {
// let i = 0;
// let interval = setInterval(() => {
// npl.sendOutput(`npl${i} from contract`);
// if (i == 5) {
// clearInterval(interval);
// npl.closeNplChannel();
// }
// i++;
// }, 500);
// npl.events.on("message", msg => {
// if (msg) {
// console.log(msg);
// }
// });
// }
//console.log("===Echo contract ended===");