EventSource()
EventSource()
コンストラクターは、リモートリソースを表す新しく作成された EventSource
を返します。
構文
new EventSource(url)
new EventSource(url, options)
引数
url
-
文字列で、イベント/メッセージを処理するリモートリソースの場所を表します。
options
省略可-
新しいコネクションを構成するオプションを提供します。可能な項目は次のとおりです。
withCredentials
省略可-
論理値で、既定値は
false
であり、 CORS の資格情報をinclude
に設定すべきかどうかを示します。
例
const evtSource = new EventSource('sse.php');
const eventList = document.querySelector('ul');
evtSource.onmessage = (e) => {
const newElement = document.createElement("li");
newElement.textContent = `message: ${e.data}`;
eventList.appendChild(newElement);
}
メモ: 完全な例は GitHub で見られます。 PHP を用いた簡単な SSE のデモ を参照してください。
仕様書
Specification |
---|
HTML Standard # dom-eventsource-dev |
ブラウザーの互換性
BCD tables only load in the browser