Skip to main content

parentPort

与父进程通信的接口。

进程:工具进程

🌐 Process: Utility

parentPort 是一个 事件触发器
该对象不会从 '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!`)
})

事件

🌐 Events

parentPort 对象会触发以下事件:

🌐 The parentPort object emits the following events:

Event: 'message'

返回:

🌐 Returns:

  • messageEvent 对象
    • data 任何
    • ports MessagePortMain[]

当进程接收到消息时触发。此端口接收到的消息将会排队,直到为此事件注册了处理程序。

🌐 Emitted when the process receives a message. Messages received on this port will be queued up until a handler is registered for this event.

方法

🌐 Methods

parentPort.postMessage(message)

  • message 任何

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

🌐 Sends a message from the process to its parent.