1. 9 コミュニケーション
    1. 9.1 MessageEventインターフェイス

9 コミュニケーション

9.1 MessageEventインターフェイス

MessageEvent

Support in all current engines.

Firefox4+Safari4+Chrome1+
Opera10.6+Edge79+
Edge (Legacy)12+Internet Explorer9+
Firefox Android4+Safari iOS3+Chrome Android18+WebView Android37+Samsung Internet1.0+Opera Android11+

サーバー送信イベントweb sockets文書をまたがるメッセージングチャンネルメッセージング、およびブロードキャストチャンネルのメッセージは、messageイベントにMessageEventインターフェイスを使用する:

[Exposed=(Window,Worker,AudioWorklet)]
interface MessageEvent : Event {
  constructor(DOMString type, optional MessageEventInit eventInitDict = {});

  readonly attribute any data;
  readonly attribute USVString origin;
  readonly attribute DOMString lastEventId;
  readonly attribute MessageEventSource? source;
  readonly attribute FrozenArray<MessagePort> ports;

  undefined 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;
event.data

MessageEvent/data

Support in all current engines.

Firefox4+Safari4+Chrome1+
OperaYesEdge79+
Edge (Legacy)12+Internet Explorer9+
Firefox AndroidYesSafari iOS3+Chrome AndroidYesWebView Android37+Samsung InternetYesOpera AndroidYes

メッセージのデータを返す。

event.origin

MessageEvent/origin

Support in all current engines.

Firefox4+Safari4+Chrome1+
OperaYesEdge79+
Edge (Legacy)12+Internet Explorer9+
Firefox Android4+Safari iOS3+Chrome AndroidYesWebView Android37+Samsung InternetYesOpera AndroidYes

サーバー送信イベントおよび文書をまたがるメッセージングの場合、メッセージの生成元を返す。

event.lastEventId

MessageEvent/lastEventId

Support in all current engines.

Firefox4+Safari4+Chrome1+
OperaYesEdge79+
Edge (Legacy)17+Internet Explorer9+
Firefox AndroidYesSafari iOS3+Chrome AndroidYesWebView Android37+Samsung InternetYesOpera AndroidYes

サーバー送信イベント最後のイベントID文字列を返す。

event.source

MessageEvent/source

Support in all current engines.

Firefox55+Safari4+Chrome1+
Opera12.1+Edge79+
Edge (Legacy)12+Internet ExplorerNo
Firefox Android55+Safari iOS3+Chrome Android18+WebView Android37+Samsung Internet1.0+Opera Android12.1+

SharedWorkerGlobalScopeオブジェクトで発火したconnectイベントで、文書をまたがるメッセージング用のソースウィンドウのWindowProxy、およびアタッチされているMessagePortを返す。

event.ports

MessageEvent/ports

Support in all current engines.

Firefox4+Safari4+Chrome1+
OperaYesEdge79+
Edge (Legacy)12+Internet Explorer9+
Firefox AndroidYesSafari iOS3+Chrome AndroidYesWebView Android37+Samsung InternetYesOpera AndroidYes

文書をまたがるメッセージングおよびチャネルメッセージングの場合、メッセージとともに送信された MessagePort配列を返す。

The data attribute must return the value it was initialized to. It represents the message being sent.

The origin attribute must return the value it was initialized to. It represents, in server-sent events and cross-document messaging, the origin of the document that sent the message (typically the scheme, hostname, and port of the document, but not its path or fragment).

The lastEventId attribute must return the value it was initialized to. It represents, in server-sent events, the last event ID string of the event source.

The source attribute must return the value it was initialized to. It represents, in cross-document messaging, the WindowProxy of the browsing context of the Window object from which the message came; and in the connect events used by shared workers, the newly connecting MessagePort.

The ports attribute must return the value it was initialized to. It represents, in cross-document messaging and channel messaging, the MessagePort array being sent.

The initMessageEvent(type, bubbles, cancelable, data, origin, lastEventId, source, ports) method must initialize the event in a manner analogous to the similarly-named initEvent() method. [DOM]

さまざまなAPI(WebSocketEventSourceなど)は、MessagePort APIを使用せずに、messageイベントにMessageEventインターフェイスを使用する。