EventSource()
EventSource
()
コンストラクタは、リモートリソースを表す新しく作成された EventSource
を返します。
構文
eventSource = new EventSource(url, configuration);
引数
url
- イベント/メッセージを処理するリモートリソースの場所を表す
USVString
。 configuration
省略可- 新しい接続を構成するオプションを提供します。可能なエントリは次のとおりです:
withCredentials
、デフォルトはfalse
で、CORS に証明書を含める
かどうかを指定します。
例
var evtSource = new EventSource('sse.php');
var eventList = document.querySelector('ul');
evtSource.onmessage = function(e) {
var newElement = document.createElement("li");
newElement.textContent = "message: " + e.data;
eventList.appendChild(newElement);
}
メモ: 完全な例を GitHub から見つけることができます — PHP を用いた簡単な SSE のデモ を参照。
仕様
仕様 | ステータス | Comment |
---|---|---|
HTML Living Standard EventSource() の定義 |
現行の標準 | 初期定義 |
ブラウザ互換性
BCD tables only load in the browser