[13] [DFN[[CODE(DOMi)@en[MessageEvent]]]] は、[[サーバー]]や他の[[起源]]からメッセージを受信した時の[[イベント]]で使われる[[イベントインターフェイス]]です。

* 仕様書

[REFS[
- [12] [CITE@en-US-x-hixie[HTML Standard]] ([TIME[2016-12-13 09:04:33 +09:00]]) <https://html.spec.whatwg.org/#messageevent>
]REFS]

* インターフェイス

[27] [[文書環境]]、[[ワーカー環境]]、 [CODE[AudioWorklet]] に[[晒され]]ています
[SRC[>>12]]。

* コンストラクター

[15] [[イベントコンストラクター]]を持ちます [SRC[>>12]]。

* メンバー

[14] [CODE(DOMi)@en[MessageEvent]] [[インターフェイス]]は、
[CODE(DOMi)@en[Event]] [[インターフェイス]]の[[メンバー][インターフェイスメンバー]]に加えて、
次の[[メンバー][インターフェイスメンバー]]を持ちます。
[CODE(DOMi)@en[MessageEventInit]] [[辞書]]も
[CODE(DOMi)@en[MessageEventInit]] [[辞書]]の[[メンバー][辞書メンバー]]に加えて、
次の[[メンバー][辞書メンバー]]を持ちます。

[FIG(short list)[
- [CODE(DOMa)@en[data][MessageEvent]]
- [CODE(DOMa)@en[origin][MessageEvent]]
- [CODE(DOMa)@en[lastEventId]]
- [CODE(DOMa)@en[source][MessageEvent]]
- [CODE(DOMa)@en[ports]]
]FIG]

[16] [CODE(DOMi)@en[MessageEvent]] [[インターフェイス]]は、更に次の[[メンバー][インターフェイスメンバー]]を持ちます。
[FIG(short list)[
- [CODE(DOMm)@en[initMessageEvent]]
]FIG]

[11] [CODE(DOMi)@en[MessageEvent]] [[インターフェイス]]は、色々な[[イベント]]に使われます。
[[IDL属性]]は、各用途で次のような値に設定されます。

[FIG(table)[

:x: 場面
:target: [F[イベント対象]]
:t: [F[イベント型]]
:d: [F[[CODE(DOMa)@en[data][MessageEvent]]]]
:o: [F[[CODE(DOMa)@en[origin][MessageEvent]]]]
:l: [F[[CODE(DOMa)@en[lastEventId]]]]
:s: [F[[CODE(DOMa)@en[source][MessageEvent]]]]
:p: [F[[CODE(DOMa)@en[ports]]]]

:target: [CODE(DOMi)@en[EventSource]]
:x: [[SSE]] のデータ受信
:t: [CODE(DOMe)@en[message][onmessage]]
:d: 受信したデータ
:o: 接続先サーバーの[[起源]]
:l: ○

:target: [CODE(DOMi)@en[WebSocket]]
:x: [[WebSocketメッセージ受信]]
:t: [CODE(DOMe)@en[message][onmessage]]
:d: 受信したデータ
:o: 接続先サーバーの[[起源]]

:target: [CODE(DOMi)@en[Window]]
:x: [CODE(DOMi)@en[Window]] の [CODE(DOMm)@en[postMessage]] 受信
:t: [CODE(DOMe)@en[message][onmessage]]
:d: 受信したデータ
:o: 送信元の[[起源]]
:p: ○
:s: ○

:target: [CODE(DOMi)@en[Window]]
:x: [CODE(DOMi)@en[Window]] の [CODE(DOMm)@en[postMessage]] 受信 (エラー)
:t: [CODE(DOMe)@en[messageerror]]
:o: 送信元の[[起源]]
:s: ○
:d: [[空文字列]]

:target: [CODE(DOMi)@en[MessagePort]]
:x: [CODE(DOMi)@en[MessagePort]] の [CODE(DOMm)@en[postMessage]] 受信
:t: [CODE(DOMe)@en[message][onmessage]]
:d: 受信したデータ
:p: ○

:target: [CODE(DOMi)@en[MessagePort]]
:x: [CODE(DOMi)@en[MessagePort]] の [CODE(DOMm)@en[postMessage]] 受信 (エラー)
:t: [CODE(DOMe)@en[messageerror]]
:d: [[空文字列]]

:target: [CODE(DOMi)@en[BroadcastChannel]]
:x: [CODE(DOMi)@en[BroadcastChannel]] の [CODE(DOMm)@en[postMessage]] 受信
:t: [CODE(DOMe)@en[message][onmessage]]
:d: 受信したデータ
:o: 送信元の[[起源]]

:target: [CODE(DOMi)@en[BroadcastChannel]]
:x: [CODE(DOMi)@en[BroadcastChannel]] の [CODE(DOMm)@en[postMessage]] 受信 (エラー)
:t: [CODE(DOMe)@en[messageerror]]
:d: [[空文字列]]
:o: 送信元の[[起源]]

:target: [CODE(DOMi)@en[SharedWorkerGlobalScope]]
:x: [[共有ワーカー]]への [CODE(DOMi)@en[SharedWorker]] 接続を検知
:t: [CODE(DOMe)@en[connect][onconnect]]
:d: [[空文字列]]
:p: ○
:s: ○

]FIG]

* [CODE(DOMi)@en[MessageEvent]] インターフェイス [CODE(DOMa)@en[data][MessageEvent]] 属性

[19] [CODE(DOMi)@en[MessageEvent]] [[インターフェイス]]と
[CODE(DOMi)@en[MessageEventInit]] [[辞書]]の
[DFN[[CODE(DOMa)@en[data][MessageEvent]]]]
[[IDL属性]]/[[辞書メンバー]] [SRC[>>12]] は、
受信したデータを表します。

[20] 値は[[任意のもの][any (Web IDL)]]です [SRC[>>12]]。
[[辞書]]での既定値は [CODE[null]] です [SRC[>>12]]。

* [CODE(DOMi)@en[MessageEvent]] インターフェイス [CODE(DOMa)@en[source][MessageEvent]] 属性

[21] [CODE(DOMi)@en[MessageEvent]] [[インターフェイス]]と
[CODE(DOMi)@en[MessageEventInit]] [[辞書]]の
[DFN[[CODE(DOMa)@en[source][MessageEvent]]]]
[[IDL属性]]/[[辞書メンバー]] [SRC[>>12]] は、
データの送信者を表します。

[22] 値は [CODE(DOMi)@en[MessageEventSource]] または [CODE[null]] です [SRC[>>12]]。
[[辞書]]における既定値は [CODE[null]] です。

[18] [DFN[[CODE(DOMi)@en[MessageEventSource]]]] は、
次の[[いずれかを表す][or (Web IDL)]]と[[型定義]]されています [SRC[>>12]]。
[FIG(short list)[
- [CODE(DOMi)@en[WindowProxy]]
- [CODE(DOMi)@en[MessagePort]]
- [CODE(DOMi)@en[ServiceWorker]]
]FIG]

* [CODE(DOMi)@en[MessageEvent]] インターフェイス [CODE(DOMa)@en[origin][MessageEvent]] 属性

[21] [CODE(DOMi)@en[MessageEvent]] [[インターフェイス]]と
[CODE(DOMi)@en[MessageEventInit]] [[辞書]]の
[DFN[[CODE(DOMa)@en[origin][MessageEvent]]]]
[[IDL属性]]/[[辞書メンバー]] [SRC[>>12]] は、
データの送信者の[[起源]]を表します。

[22] 値は [CODE(IDL)@en[USVString]] です [SRC[>>12]]。
[[辞書]]における既定値は[[空文字列]]です [SRC[>>12]]。

[23] [[起源]]の[F[直列化][ASCII直列化]]が設定されるか、[[空文字列]]が設定されます。

* 歴史

[1] [CITE@en[Web Applications 1.0 r8455     Make WebSocket binary data ArrayBuffers mutable, for consistency with XHR.]]
( ([TIME[2014-02-04 08:17:00 +09:00]] 版))
<http://html5.org/tools/web-apps-tracker?from=8454&to=8455>

[2] [CITE@en[Web Applications 1.0 r8589     Instead of MessageEvent.channel, use MessageEvent.target.name.]]
( ([TIME[2014-04-30 09:04:00 +09:00]] 版))
<http://html5.org/tools/web-apps-tracker?from=8588&to=8589>

[3] [CITE@en[Re: ''''''[''''''whatwg'''''']'''''' MessageEvent and ports]]
([[Jonas Sicking]] 著, [TIME[2015-03-07 02:03:07 +09:00]] 版)
<https://lists.w3.org/Archives/Public/public-whatwg-archive/2015Mar/0008.html>

[4] [CITE@en[Write structured clone algorithm in terms of ECMAScript · whatwg/html@bfb960c]]
([TIME[2016-03-02 16:39:01 +09:00]] 版)
<https://github.com/whatwg/html/commit/bfb960c938580c95e77365e614218b952f96375b>

[5] [CITE@en[Editorial: define Event attribute defaults through IDL · whatwg/html@0be0229]]
([TIME[2016-03-23 21:16:14 +09:00]] 版)
<https://github.com/whatwg/html/commit/0be02299f128cbb7c65a7ce259fdd838fd44a4b5>

[6] [CITE@en[Use FrozenArray for Navigator#languages and MessageEvent#ports · whatwg/html@e4df68a]]
([TIME[2016-03-25 13:51:21 +09:00]] 版)
<https://github.com/whatwg/html/commit/e4df68a41b86753c7fcdd0d8ea4615f63ffc87e9>

[7] [CITE@en[Make MessageEvent's ports attribute non-nullable]]
([[cdumez]]著, [TIME[2016-10-11 07:09:55 +09:00]])
<https://github.com/whatwg/html/commit/df2c0c448612d5b8ab85ad3c6ce0255ee11c0b01>

[8] [CITE@en[Specify the realm for the SSE MessageEvent]]
([[domenic]]著, [TIME[2016-10-28 02:17:38 +09:00]])
<https://github.com/whatwg/html/commit/91d19d647433203f1f319e2afe76e63ceae646ca>

[9] [CITE@en[Enable MessageEvent to supplant ServiceWorkerMessageEvent]]
([[annevk]]著, [TIME[2016-11-11 01:55:35 +09:00]])
<https://github.com/whatwg/html/commit/8dd63c05ddd950b28b58bdb9767724afd77e092f>

[10] [CITE@en[Add ServiceWorkerContainer target to MessageEvent]]
([[jungkees]]著, [TIME[2016-11-15 09:03:31 +09:00]])
<https://github.com/whatwg/html/commit/6531e0982385de7f21b9a585dd53ad3bfc731043>

[17] [CITE@en[Enable MessageEvent to supplant ServiceWorkerMessageEvent]]
([[annevk]]著, [TIME[2016-11-11 01:55:35 +09:00]])
<https://github.com/whatwg/html/commit/8dd63c05ddd950b28b58bdb9767724afd77e092f>

[24] [CITE@en[Define messageerror]]
([[annevk]]著, [TIME[2017-04-13 20:36:23 +09:00]])
<https://github.com/whatwg/html/commit/25a94f606e983534a863a0559d65bd29bb940a02>

[25] [CITE@en[Remove Unicode serialization of an origin]]
([[annevk]]著, [TIME[2017-05-18 13:24:49 +09:00]])
<https://github.com/whatwg/html/commit/59ebd9c094d9d532458a9ee61f307bf41bc70811>

[26] [CITE@en[Expose MessagePort to AudioWorklet]]
([[hoch]]著, [TIME[2017-10-03 17:07:58 +09:00]])
<https://github.com/whatwg/html/commit/458347c0d677dbd6e2414d6f435c77fbb9c9eab1>

[28] [CITE@en[Expose MessagePort to AudioWorklet · Issue #3081 · whatwg/html]]
([TIME[2017-10-04 17:06:13 +09:00]])
<https://github.com/whatwg/html/issues/3081>

[29] [CITE@en[Fix 3081: Expose MessagePort in AudioWorklet by hoch · Pull Request #3082 · whatwg/html]]
([TIME[2017-10-04 17:06:44 +09:00]])
<https://github.com/whatwg/html/pull/3082>

[30] [CITE@en[22646 – So if we want to say on a per-object basis whether it ought to be exposed to workers (which I think is useful) lets introduce "exposed to document environments", "exposed to worker environments", and '''['''...''']''']]
([TIME[2017-12-08 16:17:25 +09:00]])
<https://www.w3.org/Bugs/Public/show_bug.cgi?id=22646>

[31] [CITE@en[Use ASCII serialization for origins (#1151)]]
([[jungkees]]著, [TIME[2019-06-14 03:09:32 +09:00]])
<https://github.com/w3c/ServiceWorker/commit/a47c6d86c1436a4fff573d2a5da92b0e14891fa2>

[32] [CITE@en[Use ASCII serialization for origins by jungkees · Pull Request #1151 · w3c/ServiceWorker]]
([TIME[2020-01-12 20:28:37 +09:00]])
<https://github.com/w3c/ServiceWorker/pull/1151>

[33] [CITE@en[Use ASCII serialization for origins · Issue #1142 · w3c/ServiceWorker]]
([TIME[2020-01-12 20:29:13 +09:00]])
<https://github.com/w3c/ServiceWorker/issues/1142>