MessageEvent 接口服务器发送的事件,
Web sockets, 跨文档通信,
通道通信, 和
广播通道 中的
消息 使用 MessageEvent 接口:
[Constructor(DOMString type, optional MessageEventInit eventInitDict), Exposed=(Window,Worker,AudioWorklet)]
interface MessageEvent : Event {
readonly attribute any data;
readonly attribute USVString origin;
readonly attribute DOMString lastEventId;
readonly attribute MessageEventSource? source;
readonly attribute FrozenArray<MessagePort> ports;
void initMessageEvent(DOMString type, optional boolean bubbles = false, optional boolean cancelable = false, optional any data = null, optional USVString origin = "", optional DOMString lastEventId = "", optional MessageEventSource? source = null, optional sequence<MessagePort> ports = []);
};
dictionary MessageEventInit : EventInit {
any data = null;
USVString origin = "";
DOMString lastEventId = "";
MessageEventSource? source = null;
sequence<MessagePort> ports = [];
};
typedef (WindowProxy or MessagePort or ServiceWorker) MessageEventSource;
dataSupport in all current engines.
返回消息的数据。
originSupport in all current engines.
lastEventIdSupport in all current engines.
返回 上一个事件 ID 字符串, 用于 服务器发送的事件。
sourceSupport in all current engines.
返回源 window 的 WindowProxy, 用于
跨文档通信,
挂载 MessagePort,
以及在 SharedWorkerGlobalScope 对象上触发的
connect 事件中。
portsSupport in all current engines.
返回与消息同时发送的 MessagePort 数组, 用于
跨文档通信 和
通道通信。
data 属性必须返回它被初始化的值。
表示被发送的消息。
origin 属性必须返回它被初始化的值。
在 服务器发送的事件 和
跨文档通信 中,
它表示发送消息的文档的 源
(通常是文档的 scheme, 主机名和端口,
不包括它的路径和 fragment)。
lastEventId
属性必须返回它被初始化的值。在 服务器发送的事件 中,
它表示事件源的
上一个事件 ID 字符串。
source 属性必须返回它被初始化的值。
在 跨文档通信 中,
它表示发送消息的 Window 对象的
浏览上下文 的 WindowProxy;
在 共享 Worker 使用的
connect 事件中,它表示
正在连接的新 MessagePort。
ports 属性必须返回它被初始化的值。
在 跨文档通信 和
通道通信 中它表示,
被发送的 MessagePort 数组。
initMessageEvent()
方法初始化事件的方式必须与命名相似的 initEvent() 方法类似。
[DOM]
很多 API (例如 WebSocket, EventSource)都
在它们的 message 事件上直接使用
MessageEvent 接口,而没有使用 MessagePort API。