'listening' 事件


从工作线程调用 listen() 后,当服务器上触发 'listening' 事件时,则主进程中的 cluster 事件也将触发 'listening' 事件。

事件句柄使用两个参数执行,worker 包含工作进程对象,address 对象包含以下连接属性:addressportaddressType。 如果工作进程正在监听多个地址,则这将非常有用。

cluster.on('listening', (worker, address) => {
  console.log(
    `A worker is now connected to ${address.address}:${address.port}`);
});

addressType 是以下之一:

  • 4 (TCPv4)
  • 6 (TCPv6)
  • -1 (Unix 域套接字)
  • 'udp4' or 'udp6' (UDP v4 或 v6)

After calling listen() from a worker, when the 'listening' event is emitted on the server a 'listening' event will also be emitted on cluster in the master.

The event handler is executed with two arguments, the worker contains the worker object and the address object contains the following connection properties: address, port and addressType. This is very useful if the worker is listening on more than one address.

cluster.on('listening', (worker, address) => {
  console.log(
    `A worker is now connected to ${address.address}:${address.port}`);
});

The addressType is one of:

  • 4 (TCPv4)
  • 6 (TCPv6)
  • -1 (Unix domain socket)
  • 'udp4' or 'udp6' (UDP v4 or v6)