Skip to main content

MessageChannelMain

MessageChannelMain is the main-process-side equivalent of the DOM MessageChannel object. MessageChannelMain是DOM MessageChannel对象的主要进程端等价物。Its singular function is to create a pair of connected MessagePortMain objects.它的奇异功能是创建一对连接的MessagePortMain对象。

See the Channel Messaging API documentation for more information on using channel messaging.有关使用通道消息传递的更多信息,请参阅通道消息传递API文档。

Class: MessageChannelMain

Channel interface for channel messaging in the main process.主进程中用于通道消息传递的通道接口。

Process:进程:Main

Example:示例:

// Main process
const { MessageChannelMain } = require('electron')
const { port1, port2 } = new MessageChannelMain()
w.webContents.postMessage('port', null, [port2])
port1.postMessage({ some: 'message' })

// Renderer process
const { ipcRenderer } = require('electron')
ipcRenderer.on('port', (e) => {
// e.ports is a list of ports sent along with this message
e.ports[0].on('message', (messageEvent) => {
console.log(messageEvent.data)
})
})

Instance Properties实例属性

channel.port1

A MessagePortMain property.MessagePortMain属性。

channel.port2

A MessagePortMain property.MessagePortMain属性。