MessagePort Class
A message port (https://html.spec.whatwg.org/multipage/web-messaging.html#message-ports): the messages posted to a port are delivered to its entangled port, through the event loop of the relevant global object of the entangled port, once its port message queue is enabled (by MessagePort.Start or by setting MessagePort.OnMessage). The entangled ports may belong to different agents (e.g. a window and a worker): the ports can be used from the threads of their agents.
public sealed class MessagePort : EventTargetRemarks
The relevant global object of a port is the current global object when it is created (e.g. the window of the script that creates the channel); the messages sent to a port without relevant global object are not delivered. The messages are serialized by the script engine of the global object (structured clone); without script engine, only the .NET primitive values and the serializable platform objects can be sent. A port can be transferred to another realm (the transfer list of MessagePort.PostMessage, Worker.PostMessage...).
See also
- Inheritance
EventTargetMessagePort
Properties
| Name | Description |
|---|---|
| OnMessage | Gets or sets the handler of the message event; setting it enables the port message queue. |
| OnMessageError | Gets or sets the event handler of the messageerror event. |
OnMessage
MessageChannel.csGets or sets the handler of the message event; setting it enables the port message queue.
OnMessageError
MessageChannel.csGets or sets the event handler of the messageerror event.
Methods
| Name | Description |
|---|---|
| Close() | Closes the port: it is disentangled (https://html.spec.whatwg.org/multipage/web-messaging.html#dom-messageport-close). |
| PostMessage(object?, StructuredSerializeOptions?) | Posts a message to the entangled port (https://html.spec.whatwg.org/multipage/web-messaging.html#dom-messageport-postmessage-options). |
| PostMessage(object?, IReadOnlyList<object>) | Posts a message to the entangled port (https://html.spec.whatwg.org/multipage/web-messaging.html#dom-messageport-postmessage). |
| Start() | Enables the port message queue: the pending messages are delivered (https://html.spec.whatwg.org/multipage/web-messaging.html#dom-messageport-start). |
Close()
MessageChannel.csCloses the port: it is disentangled (https://html.spec.whatwg.org/multipage/web-messaging.html#dom-messageport-close).
public void Close()PostMessage overloads
PostMessage(object?, StructuredSerializeOptions?)
MessageChannel.csPosts a message to the entangled port (https://html.spec.whatwg.org/multipage/web-messaging.html#dom-messageport-postmessage-options).
public void PostMessage(object? message, StructuredSerializeOptions? options = null)Parameters
messageobject?- The message: a value of the script engine, a .NET primitive value or a serializable platform object, or null for
undefined. optionsStructuredSerializeOptions?- The objects to transfer.
See also
PostMessage(object?, IReadOnlyList<object>)
MessageChannel.csPosts a message to the entangled port (https://html.spec.whatwg.org/multipage/web-messaging.html#dom-messageport-postmessage).
public void PostMessage(object? message, IReadOnlyList<object> transfer)Parameters
messageobject?- The message: a value of the script engine, a .NET primitive value or a serializable platform object, or null for
undefined. transferIReadOnlyList<object>- The objects to transfer (the
ArrayBufferobjects of the script engine, and the message ports).
See also
Start()
MessageChannel.csEnables the port message queue: the pending messages are delivered (https://html.spec.whatwg.org/multipage/web-messaging.html#dom-messageport-start).
public void Start()