根据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);
};