事件:'listening'


【Event: 'listening'

在工作线程中调用 listen() 后,当服务器发出 'listening' 事件时,主进程中的 cluster 也会发出 'listening' 事件。

【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 primary.】

事件处理程序会使用两个参数执行,其中 worker 包含 worker 对象,address 对象包含以下连接属性:addressportaddressType。如果 worker 在多个地址上监听,这非常有用。

【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}`);
}); 

addressType 可以是以下之一:

【The addressType is one of:】

  • 4(TCPv4)
  • 6(TCPv6)
  • -1(Unix域套接字)
  • 'udp4''udp6'(UDPv4 或 UDPv6)