EventSource 接口用于接收服务器发送的事件。它通过HTTP连接到一个服务器,以text/event-stream 格式接收事件, 不关闭连接。

构造函数

EventSource()
以指定的USVString创建一个新的 EventSource

属性

此接口从其父接口EventTarget继承属性。

EventSource.onerror
是一个 EventHandler,当发生错误时被调用,并且在此对象上派发 error 事件。
EventSource.onmessage
是一个 EventHandler,当收到一个 message事件,即消息来自源头时被调用。
EventSource.onopen
是一个 EventHandler,当收到一个 open 事件,即连接刚打开时被调用。
EventSource.readyState 只读
一个 unsigned short 值,代表连接状态。可能值是CONNECTING (0), OPEN (1), 或者 CLOSED (2)。
EventSource.url 只读
 一个DOMString,代表源头的URL。

方法

此接口从其父接口EventTarget继承方法。

EventSource.close()
如果存在,则关闭连接,并且设置readyState 属性为 CLOSED。如果连接已经被关闭,此方法不做任何事。
 

规范

规范 状态 注释
HTML Living Standard
EventSource
Living Standard  

浏览器兼容性

 

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung Internet
Basic supportChrome Full support 6Edge No support NoFirefox Full support 6IE No support NoOpera Full support YesSafari Full support 5WebView Android Full support YesChrome Android Full support 18Edge Mobile No support NoFirefox Android Full support 45Opera Android Full support 12Safari iOS Full support 5Samsung Internet Android ?
Available in workersChrome Full support YesEdge No support NoFirefox Full support 53IE No support NoOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile No support NoFirefox Android Full support 53Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android ?
EventSource() constructorChrome Full support 9Edge No support NoFirefox Full support 6IE No support NoOpera Full support 11Safari Full support 5WebView Android Full support YesChrome Android Full support 18Edge Mobile No support NoFirefox Android Full support 6Opera Android Full support 11Safari iOS Full support 5Samsung Internet Android ?
closeChrome Full support 6Edge No support NoFirefox Full support 6IE No support NoOpera Full support YesSafari Full support 5WebView Android Full support YesChrome Android Full support 18Edge Mobile No support NoFirefox Android Full support 45Opera Android Full support 12Safari iOS Full support 5Samsung Internet Android ?
onerrorChrome Full support 6Edge No support NoFirefox Full support 6IE No support NoOpera Full support YesSafari Full support 5WebView Android Full support YesChrome Android Full support 18Edge Mobile No support NoFirefox Android Full support 45Opera Android Full support 12Safari iOS Full support 5Samsung Internet Android ?
onmessageChrome Full support 6Edge No support NoFirefox Full support 6IE No support NoOpera Full support YesSafari Full support 5WebView Android Full support YesChrome Android Full support 18Edge Mobile No support NoFirefox Android Full support 45Opera Android Full support 12Safari iOS Full support 5Samsung Internet Android ?
onopenChrome Full support 6Edge No support NoFirefox Full support 6IE No support NoOpera Full support YesSafari Full support 5WebView Android Full support YesChrome Android Full support 18Edge Mobile No support NoFirefox Android Full support 45Opera Android Full support 12Safari iOS Full support 5Samsung Internet Android ?
readyStateChrome Full support 6Edge No support NoFirefox Full support 6IE No support NoOpera Full support YesSafari Full support 5WebView Android Full support YesChrome Android Full support 18Edge Mobile No support NoFirefox Android Full support 45Opera Android Full support 12Safari iOS Full support 5Samsung Internet Android ?
urlChrome Full support 6Edge No support NoFirefox Full support 6IE No support NoOpera Full support YesSafari Full support 5WebView Android Full support YesChrome Android Full support 18Edge Mobile No support NoFirefox Android Full support 45Opera Android Full support 12Safari iOS Full support 5Samsung Internet Android ?
withCredentialsChrome Full support 6Edge No support NoFirefox Full support 6IE No support NoOpera Full support YesSafari Full support 5WebView Android Full support YesChrome Android Full support 18Edge Mobile No support NoFirefox Android Full support 45Opera Android Full support 12Safari iOS Full support 5Samsung Internet Android ?

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown

 

 

相关链接

文档标签和贡献者

标签: 
此页面的贡献者: Jack.Works, xlaoyu, xgqfrms-GitHub, kameii, ziyunfei
最后编辑者: Jack.Works,