EventSource
()
コンストラクタは、リモートリソースを表す新しく作成された EventSource
を返します。
構文
eventSource = new EventSource(url, configuration);
引数
url
- イベント/メッセージを処理するリモートリソースの場所を表す
USVString
。 configuration
Optional- 新しい接続を構成するオプションを提供します。可能なエントリは次のとおりです:
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
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.