Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

The pjs.session.onEnd function can be defined in any PJS module and allows users to execute custom code when a browser session is ended. This function is useful in situations where you need to perform cleanup operations, log data, or trigger any specific actions after the browser session ends.

Example

This example shows how the pjs.session.onEnd hook can be used to clean up resources when the browser is closed.

const { spawn } = require("child_process");

const childProcesses = [];

function main() {
  pjs.defineDisplay("display", "pjs-1118-ws.json");

  const fields = { exit: false };

  pjs.session.onEnd = onEnd;

  while (!fields.exit) {
    display.screen.execute(fields);
    if (fields.spawn) {
      const cp = spawn("node", ["modules/pjs-1118-ws/interval.js"], { stdio: "inherit" });
      childProcesses.push(cp);
    }
  }
  flags.LR = true;
}


function onEnd() {
  while (childProcesses.length > 0) {
    const childProcess = childProcesses[0];
    if (childProcess) {
      childProcess.kill("SIGKILL");
    }
    childProcesses.shift();
  }
}
  • No labels