Skip to main content

MessagePortMain

MessagePortMain is the main-process-side equivalent of the DOM MessagePort object. 是DOM MessagePort对象的主要进程端等效对象。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. 它的行为与DOM版本类似,只是它使用Node.js EventEmitter事件系统,而不是DOM EventTarget系统。This means you should use port.on('message', ...) to listen for events, instead of port.onmessage = ... or port.addEventListener('message', ...)这意味着您应该使用port.on('message', ...)来侦听事件,而不是port.onmessage = ...port.addEventListener('message', ...)

See the Channel Messaging API documentation for more information on using channel messaging.有关使用通道消息传递的更多信息,请参阅通道消息传递API文档。

MessagePortMain is an [EventEmitter][event-emitter].EventEmitter

Class: MessagePortMain

Port interface for channel messaging in the main process.主进程中用于通道消息传递的端口接口。

Process:进程:Main
This class is not exported from the 'electron' module. 此类不是从'electron'模块导出的。It is only available as a return value of other methods in the Electron API.它只能作为Electron API中其他方法的返回值使用。

Instance Methods实例方法

port.postMessage(message, [transfer])

  • message any
  • 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实例事件

Event: 'message'

Returns:返回:

  • messageEvent Object
    • data any
    • ports MessagePortMain[]

Emitted when a MessagePortMain object receives a message.当MessagePortMain对象接收到消息时发出。

Event: 'close'

Emitted when the remote end of a MessagePortMain object becomes disconnected.当MessagePortMain对象的远程端断开连接时发出。