使用 process.send 发出消息时的第二个参数
Second argument when emitting messages with process.send
main.js
let child = require('child_process').fork('./child.js');
child.on('message', function () {
console.log('message', arguments)
});
child.js
process.send({ ... });
似乎使用两个参数调用处理程序:
{ '0': { ... }, '1': undefined }
我指望 message
侦听器中的参数长度,因此参数的确切数量很重要。
第二个参数是什么 (undefined
)?它是否在所有 Node.js 版本中无条件存在?
fine manual 状态:
Event: 'message'
• message
<Object>
a parsed JSON object or primitive value.
• sendHandle
<Handle>
a net.Socket
or net.Server
object, or undefined.
如果您在同一页面上搜索 sendHandle,您最终会得到 here,具体来说:
The optional sendHandle
argument that may be passed to child.send()
is for passing a TCP server or socket object to the child process. The child will receive the object as the second argument passed to the callback function registered on the process.on('message')
event.
但是,在您的情况下,您正在从另一个方向发送消息,从 child 到服务器,在这种情况下发送句柄没有意义。因此,我的猜测是,在那种情况下,第二个参数将 always 为 undefined
(但仍会存在)。
main.js
let child = require('child_process').fork('./child.js');
child.on('message', function () {
console.log('message', arguments)
});
child.js
process.send({ ... });
似乎使用两个参数调用处理程序:
{ '0': { ... }, '1': undefined }
我指望 message
侦听器中的参数长度,因此参数的确切数量很重要。
第二个参数是什么 (undefined
)?它是否在所有 Node.js 版本中无条件存在?
fine manual 状态:
Event: 'message'
•
message
<Object>
a parsed JSON object or primitive value.•
sendHandle
<Handle>
anet.Socket
ornet.Server
object, or undefined.
如果您在同一页面上搜索 sendHandle,您最终会得到 here,具体来说:
The optional
sendHandle
argument that may be passed tochild.send()
is for passing a TCP server or socket object to the child process. The child will receive the object as the second argument passed to the callback function registered on theprocess.on('message')
event.
但是,在您的情况下,您正在从另一个方向发送消息,从 child 到服务器,在这种情况下发送句柄没有意义。因此,我的猜测是,在那种情况下,第二个参数将 always 为 undefined
(但仍会存在)。