worker.send(message[, sendHandle[, options]][, callback])
message
<Object>sendHandle
<Handle>options
<Object>options
参数(如果存在)是用于参数化某些类型句柄的发送的对象。options
支持以下属性:keepOpen
<boolean> 当传入net.Socket
实例时可以使用的值。 当为true
时,套接字在发送过程中保持打开状态。 默认值:false
。
callback
<Function>- 返回: <boolean>
向工作进程或主进程发送消息,可选择使用句柄。
在主进程中,这会向特定的工作进程发送消息。
它与 ChildProcess.send()
相同。
在工作进程中,这会向主进程发送消息。
它与 process.send()
相同。
此示例将回显来自主进程的所有消息:
if (cluster.isPrimary) {
const worker = cluster.fork();
worker.send('hi there');
} else if (cluster.isWorker) {
process.on('message', (msg) => {
process.send(msg);
});
}
message
<Object>sendHandle
<Handle>options
<Object> Theoptions
argument, if present, is an object used to parameterize the sending of certain types of handles.options
supports the following properties:keepOpen
<boolean> A value that can be used when passing instances ofnet.Socket
. Whentrue
, the socket is kept open in the sending process. Default:false
.
callback
<Function>- Returns: <boolean>
Send a message to a worker or primary, optionally with a handle.
In the primary, this sends a message to a specific worker. It is identical to
ChildProcess.send()
.
In a worker, this sends a message to the primary. It is identical to
process.send()
.
This example will echo back all messages from the primary:
if (cluster.isPrimary) {
const worker = cluster.fork();
worker.send('hi there');
} else if (cluster.isWorker) {
process.on('message', (msg) => {
process.send(msg);
});
}