跳转到主内容
Soon, @electron packages on npm will require Node.js 22 LTS. Read more on our blog.

MessageChannelMain

MessageChannelMain 是 DOM MessageChannel 对象的主进程等价对象。 Its singular function is to create a pair of connected MessagePortMain objects.

关于 channel 消息的使用文档详见 Channel Messaging API

Class: MessageChannelMain

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

Process: Main

示例:

// Main process
const{BrowserWindow,MessageChannelMain}=require('electron')

const w =newBrowserWindow()
const{ port1, port2 }=newMessageChannelMain()
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)
}
})

[!WARNING] Electron's built-in classes cannot be subclassed in user code. For more information, see the FAQ.

实例属性

channel.port1

A MessagePortMain property.

channel.port2

A MessagePortMain property.

AltStyle によって変換されたページ (->オリジナル) /