Skip to main content

MessagePortMain

MessagePortMain 是 DOM MessagePort 对象在主进程端的对应物。它的行为类似于 DOM 版本,只是它使用 Node.js 的 EventEmitter 事件系统,而不是 DOM 的 EventTarget 系统。这意味着你应该使用 port.on('message', ...) 来监听事件,而不是 port.onmessage = ...port.addEventListener('message', ...)

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

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

MessagePortMain 是一个 事件触发器

类:MessagePortMain

🌐 Class: MessagePortMain

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

进程: 主进程
此类未从 'electron' 模块导出。它仅作为 Electron API 中其他方法的返回值提供。

实例方法

🌐 Instance Methods

port.postMessage(message, [transfer])

  • message 任何
  • transfer MessagePortMain[](可选)

从端口发送消息,并可选择将对象的所有权转移到其他浏览上下文。

🌐 Sends a message from the port, and optionally, transfers ownership of objects to other browsing contexts.

port.start()

开始发送在端口上排队的消息。消息将被排队,直到调用此方法时才会发送。

🌐 Starts the sending of messages queued on the port. Messages will be queued until this method is called.

port.close()

断开端口连接,使其不再处于活动状态。

🌐 Disconnects the port, so it is no longer active.

实例事件

🌐 Instance Events

Event: 'message'

返回:

🌐 Returns:

  • messageEvent 对象
    • data 任何
    • ports MessagePortMain[]

当 MessagePortMain 对象收到消息时发出。

🌐 Emitted when a MessagePortMain object receives a message.

Event: 'close'

当 MessagePortMain 对象的远程端断开连接时发出。

🌐 Emitted when the remote end of a MessagePortMain object becomes disconnected.