parentPort
与父进程通信的接口。
进程: Utility
parentPort 是一个 EventEmitter。此对象未从 'electron' 模块导出。它仅作为 Electron API 中 process 对象的属性可用。
// Main process
const child = utilityProcess.fork(path.join(__dirname, 'test.js'))
child.postMessage({ message: 'hello' })
child.on('message', (data) => {
console.log(data) // hello world!
})
// Child process
process.parentPort.on('message', (e) => {
process.parentPort.postMessage(`${e.data} world!`)
})
事件
parentPort 对象会发出以下事件
事件: 'message'
返回
messageEvent对象dataanyportsMessagePortMain[]
当进程收到消息时发出。在此端口上收到的消息将被排队,直到为此事件注册处理程序为止。
方法
parentPort.postMessage(message)
messageany
将消息从进程发送到其父进程。