Skip to main content

MessageChannelMain

MessageChannelMain

MessageChannelMain 是 DOM MessageChannel 对象的主进程端等效项。它的奇异功能是创建一对连接的 MessagePortMain 对象。

¥MessageChannelMain is the main-process-side equivalent of the DOM MessageChannel object. Its singular function is to create a pair of connected MessagePortMain objects.

有关使用通道消息传递的更多信息,请参阅 渠道消息 API 文档。

¥See the Channel Messaging API documentation for more information on using channel messaging.

类:MessageChannelMain

¥Class: MessageChannelMain

主进程中用于通道消息传递的通道接口。

¥Channel interface for channel messaging in the main process.

进程:主进程

¥Process: Main

示例:

¥Example:

// Main process
const { BrowserWindow, MessageChannelMain } = require('electron')
const w = new BrowserWindow()
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].onmessage = (messageEvent) => {
console.log(messageEvent.data)
}
})

实例属性

¥Instance Properties

channel.port1

MessagePortMain 属性。

¥A MessagePortMain property.

channel.port2

MessagePortMain 属性。

¥A MessagePortMain property.