1.0.0 • Published 4 years ago

dcl-node-runtime v1.0.0

Weekly downloads
140
License
Apache 2.0
Repository
-
Last release
4 years ago

Node runtime for decentraland scenes

It is meant to run from a host application like this:

function setDebugRunner(wss: WebSocket.Server) {
  wss.on("connection", (ws) => {
    if (ws.protocol == "dcl-scene") {
      // dist/index.js
      const file = require.resolve("dcl-node-runtime");

      const fork = childProcess.fork(file, [], {
        // enable two way IPC
        stdio: [0, 1, 2, "ipc"],
        cwd: process.cwd(),
      });

      console.log(`> Creating scene fork #` + fork.pid);

      fork.on("close", () => {
        if (ws.readyState == ws.OPEN) {
          ws.close();
        }
      });
      fork.on("message", (message) => {
        if (ws.readyState == ws.OPEN) {
          ws.send(message);
        }
      });
      ws.on("message", (data) => fork.send(data.toString()));
      ws.on("close", () => {
        console.log("> Killing fork #" + fork.pid);
        fork.kill();
      });
    }
  });
}