MessageChannelMain

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

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

Class: MessageChannelMain

进程:主进程

示例:

  1. // Main process
  2. const { port1, port2 } = new MessageChannelMain()
  3. w.webContents.postMessage('port', null, [port2])
  4. port1.postMessage({ some: 'message' })
  5. // Renderer process
  6. const { ipcRenderer } = require('electron')
  7. ipcRenderer.on('port', (e) => {
  8. // e.ports is a list of ports sent along with this message
  9. e.ports[0].on('message', (messageEvent) => {
  10. console.log(messageEvent.data)
  11. })
  12. })

实例属性

channel.port1

A MessagePortMain property.

channel.port2

A MessagePortMain property.