EventSource
()
构造函数返回一个新建的EventSource
,它代表了一个远程资源。
语法
pc = new EventSource(url, configuration);
参数
url
- 一个
USVString
,它代表远程资源的位置 configuration
可选- 为配置新连接提供选项。可选项是:
withCredentials
,默认为false
,指示 CORS 是否应包含凭据( credentials )。
返回值
一个新建的 EventSource
对象,如果指定了configuration
, 则按其配置;否则,配置为合适的基本默认值。
示例
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 查看完整示例 — 请查看 Simple SSE demo using PHP.
规范
Specification | Status | Comment |
---|---|---|
HTML Living Standard EventSource() |
Living Standard | Initial definition |
浏览器兼容性
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help!
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 9 | 6.0 (6.0) | ? | 11 | 5 |
CORS support (withCredentials ) |
26 | 11.0 (11.0) | ? | 12 | ? |
Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | 4.4 | 6.0 (6.0) | ? | ? | ? |
CORS support (withCredentials ) |
? | 11.0 (11.0) | ? | ? | ? |