我們的志工尚未將此文章翻譯為 正體中文 (繁體) 版本。加入我們,幫忙翻譯!
The EventSource
interface is used to receive server-sent events. It connects to a server over HTTP and receives events in text/event-stream
format without closing the connection.
Constructor
EventSource()
- Creates a new
EventSource
from the givenUSVString
.
Properties
This interface also inherits properties from its parent, EventTarget
.
EventSource.onerror
- Is an
EventHandler
being called when an error occurs and theerror
event is dispatched on this object. EventSource.onmessage
- Is an
EventHandler
being called when amessage
event is received, that is when a message is coming from the source. EventSource.onopen
- Is an
EventHandler
being called when anopen
event is received, that is when the connection was just opened. EventSource.readyState
Read only- An
unsigned short
representing the state of the connection. Possible values areCONNECTING
(0
),OPEN
(1
), orCLOSED
(2
). EventSource.url
Read only- A
DOMString
representing the URL of the source.
Methods
This interface also inherits methods from its parent, EventTarget
.
EventSource.close()
- Closes the connection, if any, and sets the
readyState
attribute toCLOSED
. If the connection is already closed, the method does nothing.
Specifications
Specification | Status | Comment |
---|---|---|
WHATWG HTML Living Standard The definition of 'EventSource' in that specification. |
Living Standard |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 9 | 6.0 (6.0) | ? | 11 | 5 |
CORS support | 26 | 11.0 (11.0) | ? | 12 | ? |
Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | 4.4 | ? | ? | ? | ? |