跳到主要内容

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 对象
    • data 任意类型
    • ports MessagePortMain[]

当进程收到消息时触发。在此端口上收到的消息将在注册此事件的处理程序之前排队。

方法

parentPort.postMessage(message)

  • message 任意类型

从进程向其父级发送消息。