'use strict';

const impl = process.argv[2];

if (!impl) {
  console.error('No implementation provided');
  process.exit(1);
}

switch (impl) {
  case 'ws':
  case 'uws':
  case 'faye':
    break;
  default:
    console.error(`Implementation: ${impl} not valid`);
    process.exit(1);
}

const cluster = require('cluster');
const numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
  // Fork workers.
  for (var i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

  let onExit = () => {
    cluster.removeAllListeners('exit');
    Object.keys(cluster.workers).forEach((id) => {
      cluster.workers[id].kill();
    });
  };

  process
  .on('SIGINT', onExit)
  .on('SIGTERM', onExit);

  cluster.on('exit', (worker, code, signal) => {
    console.log(`worker ${worker.process.pid} died`);
    setTimeout(() => cluster.fork(), 1000);
  });

  cluster.on('message', (worker, msg) => {
    Object.keys(cluster.workers).forEach((id) => {
      if (parseInt(id) !== worker.id) {
        cluster.workers[id].send(msg);
      }
    });
  });
} else {
  require(`./${impl}/`);
}

'use strict';

const impl = process.argv[2];

if (!impl) {
  console.error('No implementation provided');
  process.exit(1);
}

switch (impl) {
  case 'ws':
  case 'uws':
  case 'faye':
    break;
  default:
    console.error(`Implementation: ${impl} not valid`);
    process.exit(1);
}

const cluster = require('cluster');
const numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
  const onMessage = function (msg) {
    Object.keys(cluster.workers).forEach((id) => {
      if (+id !== this.id) {
        cluster.workers[id].send(msg);
      }
    });
  };

  cluster.on('fork', (worker) => worker.on('message', onMessage));

  // Fork workers.
  for (var i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

  const onExit = () => {
    cluster.removeAllListeners('exit');
    Object.keys(cluster.workers).forEach((id) => {
      cluster.workers[id].kill();
    });
  };

  process
  .on('SIGINT', onExit)
  .on('SIGTERM', onExit);

  cluster.on('exit', (worker, code, signal) => {
    console.log(`worker ${worker.process.pid} died`);
    setTimeout(() => cluster.fork(), 1000);
  });
} else {
  require(`./${impl}/`);
}

Fix compatibility with Node.js v4.x
Fix compatibility with Node.js v4.x

The callback for the `message` event on the cluster is invoked
without the `worker` argument in Node.js v4.x.

This commit fixes the issue by using the `message` event that is
emitted on the workers.
