Skip to main content

parentPort

parentPort

与父进程通信的接口。

¥Interface for communication with parent process.

进程:实用工具

¥Process: Utility

parentPortEventEmitter。该对象不从 'electron' 模块导出。它仅作为 Electron API 中进程对象的属性提供。

¥parentPort is an EventEmitter. This object is not exported from the 'electron' module. It is only available as a property of the process object in the Electron API.

// 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:

事件:'message'

¥Event: 'message'

返回:

¥Returns:

  • messageEvent 对象

    ¥messageEvent Object

    • data 任意

      ¥data any

    • ports 消息端口主[]

      ¥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 任意

    ¥message any

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

¥Sends a message from the process to its parent.