EventSource
()
constructor returns a newly-created
EventSource
, which represents a remote resource.
eventSource = new EventSource(url, configuration);
url
USVString
that represents the location of the remote resource serving the events/messages.
configuration
可选
withCredentials
,默认为
false
, indicating if CORS should be set to
包括
credentials.
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);
}
注意 : You can find a full example on GitHub — see Simple SSE demo using PHP.
| 规范 | 状态 | 注释 |
|---|---|---|
|
HTML 实时标准
The definition of 'EventSource()' in that specification. |
实时标准 | 初始定义 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
EventSource()
构造函数
|
Chrome 9 | Edge 79 | Firefox 6 | IE No | Opera 11 | Safari 5 | WebView Android Yes | Chrome Android 18 | Firefox Android 45 | Opera Android 12 | Safari iOS 5 | Samsung Internet Android 1.0 |
CORS support (
withCredentials
)
|
Chrome 26 | Edge 79 | Firefox 11 | IE No | Opera 12 | Safari ? | WebView Android Yes | Chrome Android 26 | Firefox Android 45 | Opera Android 12 | Safari iOS ? | Samsung Internet Android 2.0 |
完整支持
不支持
兼容性未知
EventSource
EventSource()