socket.addMembership(multicastAddress[, multicastInterface])


通知内核将 multicastAddressmulticastInterface 提供的多路传送集合通过 IP_ADD_MEMBERSHIP 这个 socket 选项结合起来。 若 multicastInterface 参数未指定,则操作系统将会选择一个接口并向其添加成员。 要为所有可用的接口添加成员,可以在每个接口上调用一次 addMembership 方法。

当多个 cluster 工作进程之间共享 UDP socket 时,则 socket.addMembership() 函数必须只能被调用一次,否则将会发生 EADDRINUSE 错误:

const cluster = require('cluster');
const dgram = require('dgram');
if (cluster.isMaster) {
  cluster.fork(); // 可工作。
  cluster.fork(); // 失败并抛出 EADDRINUSE。
} else {
  const s = dgram.createSocket('udp4');
  s.bind(1234, () => {
    s.addMembership('224.0.0.114');
  });
}

Tells the kernel to join a multicast group at the given multicastAddress and multicastInterface using the IP_ADD_MEMBERSHIP socket option. If the multicastInterface argument is not specified, the operating system will choose one interface and will add membership to it. To add membership to every available interface, call addMembership multiple times, once per interface.

When sharing a UDP socket across multiple cluster workers, the socket.addMembership() function must be called only once or an EADDRINUSE error will occur:

const cluster = require('cluster');
const dgram = require('dgram');
if (cluster.isMaster) {
  cluster.fork(); // Works ok.
  cluster.fork(); // Fails with EADDRINUSE.
} else {
  const s = dgram.createSocket('udp4');
  s.bind(1234, () => {
    s.addMembership('224.0.0.114');
  });
}