Skip to main content

MessagePortMain

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

¥MessagePortMain is the main-process-side equivalent of the DOM MessagePort object. It behaves similarly to the DOM version, with the exception that it uses the Node.js EventEmitter event system, instead of the DOM EventTarget system. This means you should use port.on('message', ...) to listen for events, instead of port.onmessage = ... or port.addEventListener('message', ...)

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

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

MessagePortMainEventEmitter

¥MessagePortMain is an EventEmitter.

类:MessagePortMain

¥Class: MessagePortMain

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

¥Port interface for channel messaging in the main process.

进程:主进程
该类不是从 'electron' 模块导出的。它仅可用作 Electron API 中其他方法的返回值。

¥Process: Main
This class is not exported from the 'electron' module. It is only available as a return value of other methods in the Electron API.

实例方法

¥Instance Methods

port.postMessage(message, [transfer])

  • message 任意

    ¥message any

  • transfer MessagePortMain[](可选)

    ¥transfer MessagePortMain[] (optional)

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

¥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

事件:'message'

¥Event: 'message'

返回:

¥Returns:

  • messageEvent 对象

    ¥messageEvent Object

    • data 任意

      ¥data any

    • ports 消息端口主[]

      ¥ports MessagePortMain[]

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

¥Emitted when a MessagePortMain object receives a message.

事件:'close'

¥Event: 'close'

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

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