根据WHATWG - Server-Sent Events下面是使用EventSource接口的 API:
[Constructor(DOMString url, optional EventSourceInit eventSourceInitDict)]
interface EventSource : EventTarget {
readonly attribute DOMString url;
readonly attribute boolean withCredentials;
//....
};
withCredentials 属性必须返回它上次初始化的值。创建对象时,必须将其初始化为 false。
简单的例子:
var stocks = new EventSource("events.php");
stocks.onmessage = function (event) {
//alert(event.data);
};