EventSource Class
A connection to a server that sends events (https://html.spec.whatwg.org/multipage/server-sent-events.html#the-eventsource-interface): the response of a CORS request for a text/event-stream resource is interpreted as its bytes arrive (see DocumentResource.RemainingContentBytes), and each event is dispatched as a MessageEvent. When the response ends, the connection is reestablished after the reconnection time, with the Last-Event-ID header.
public sealed class EventSource : EventTargetRemarks
The relevant global object of the event source is the current global object when it is created (the window or the worker of the script that creates it): its events are fired by tasks of its event loop, and the reconnection time is measured by the timers of that event loop (DomEventLoop.QueueTimer). An event source created without current global object cannot connect: its EventSource.ReadyState is EventSource.Closed.
An open connection that waits for events is not an asynchronous operation of its event loop: DomEventLoop.RunUntilIdleAsync does not wait for the next event. The event sources of a document are closed when the document is unloaded or destroyed, and the ones of a worker when it ends.
See also
- Inheritance
EventTargetEventSource
Constructors
| Name | Description |
|---|---|
| EventSource(string, EventSourceInit?) | Creates an event source and starts to establish its connection (https://html.spec.whatwg.org/multipage/server-sent-events.html#dom-eventsource). |
EventSource(string, EventSourceInit?)
EventSource.csCreates an event source and starts to establish its connection (https://html.spec.whatwg.org/multipage/server-sent-events.html#dom-eventsource).
public EventSource(string url, EventSourceInit? eventSourceInitDict = null)Parameters
urlstring- The URL of the event stream, possibly relative to the API base URL of the relevant global object.
eventSourceInitDictEventSourceInit?- The options, or null.
Exceptions
DomException- A
SyntaxErrorwhen the URL is not valid.
See also
Properties
| Name | Description |
|---|---|
| OnError | Gets or sets the event handler of the error event. |
| OnMessage | Gets or sets the event handler of the message event. |
| OnOpen | Gets or sets the event handler of the open event. |
| ReadyState | Gets the state of the connection (https://html.spec.whatwg.org/multipage/server-sent-events.html#dom-eventsource-readystate): EventSource.Connecting, EventSource.Open or EventSource.Closed. |
| Url | Gets the URL of the event stream, serialized (https://html.spec.whatwg.org/multipage/server-sent-events.html#dom-eventsource-url). |
| WithCredentials | Gets whether the credentials mode of the requests is "include": the cookies are also sent to another origin. |
OnError
EventSource.csGets or sets the event handler of the error event.
OnMessage
EventSource.csGets or sets the event handler of the message event.
OnOpen
EventSource.csGets or sets the event handler of the open event.
ReadyState
EventSource.csGets the state of the connection (https://html.spec.whatwg.org/multipage/server-sent-events.html#dom-eventsource-readystate): EventSource.Connecting, EventSource.Open or EventSource.Closed.
public ushort ReadyState { get; }Gets the URL of the event stream, serialized (https://html.spec.whatwg.org/multipage/server-sent-events.html#dom-eventsource-url).
public string Url { get; }Property value
See also
WithCredentials
EventSource.csGets whether the credentials mode of the requests is "include": the cookies are also sent to another origin.
public bool WithCredentials { get; }Methods
| Name | Description |
|---|---|
| Close() | Closes the connection (https://html.spec.whatwg.org/multipage/server-sent-events.html#dom-eventsource-close): the fetch is aborted, the events that were received and not yet dispatched are dropped, and the connection is not reestablished. |
Close()
EventSource.csCloses the connection (https://html.spec.whatwg.org/multipage/server-sent-events.html#dom-eventsource-close): the fetch is aborted, the events that were received and not yet dispatched are dropped, and the connection is not reestablished.
public void Close()Constants
| Name | Description |
|---|---|
| Closed | The state of an event source whose connection is not open and is not reestablished: it failed, or EventSource.Close was called. |
| Connecting | The state of an event source whose connection has not yet been established, or was closed and is being reestablished. |
| Open | The state of an event source whose connection is open: the events are dispatched as they are received. |
Closed
The state of an event source whose connection is not open and is not reestablished: it failed, or EventSource.Close was called.
public const ushort Closed = 2Connecting
The state of an event source whose connection has not yet been established, or was closed and is being reestablished.
public const ushort Connecting = 0Open
The state of an event source whose connection is open: the events are dispatched as they are received.
public const ushort Open = 1