EventSource class
The EventSource interface is used to manage server-sent events. You can set the onmessage attribute to a JavaScript function to receive non-typed messages (that is, messages with no event field). You can also call addEventListener() to listen for events just like any other event source.
See Using server-sent events for further details.
@DomName('EventSource')
class EventSource extends EventTarget native "EventSource" {
factory EventSource(String title, {withCredentials: false}) {
var parsedOptions = {
'withCredentials': withCredentials,
};
return EventSource._factoryEventSource(title, parsedOptions);
}
@DomName('EventSource.errorEvent')
@DocsEditable
static const EventStreamProvider<Event> errorEvent = const EventStreamProvider<Event>('error');
@DomName('EventSource.messageEvent')
@DocsEditable
static const EventStreamProvider<MessageEvent> messageEvent = const EventStreamProvider<MessageEvent>('message');
@DomName('EventSource.openEvent')
@DocsEditable
static const EventStreamProvider<Event> openEvent = const EventStreamProvider<Event>('open');
@DomName('EventSource.EventSource')
@DocsEditable
static EventSource _factoryEventSource(String url, [Map eventSourceInit]) {
if (?eventSourceInit) {
return EventSource._create_1(url, eventSourceInit);
}
return EventSource._create_2(url);
}
static EventSource _create_1(url, eventSourceInit) => JS('EventSource', 'new EventSource(#,#)', url, eventSourceInit);
static EventSource _create_2(url) => JS('EventSource', 'new EventSource(#)', url);
@DomName('EventSource.CLOSED')
@DocsEditable
static const int CLOSED = 2;
@DomName('EventSource.CONNECTING')
@DocsEditable
static const int CONNECTING = 0;
@DomName('EventSource.OPEN')
@DocsEditable
static const int OPEN = 1;
@DomName('EventSource.readyState')
@DocsEditable
final int readyState;
@DomName('EventSource.url')
@DocsEditable
final String url;
@DomName('EventSource.withCredentials')
@DocsEditable
final bool withCredentials;
@JSName('addEventListener')
@DomName('EventSource.addEventListener')
@DocsEditable
void $dom_addEventListener(String type, EventListener listener, [bool useCapture]) native;
@DomName('EventSource.close')
@DocsEditable
void close() native;
@DomName('EventSource.dispatchEvent')
@DocsEditable
bool dispatchEvent(Event evt) native;
@JSName('removeEventListener')
@DomName('EventSource.removeEventListener')
@DocsEditable
void $dom_removeEventListener(String type, EventListener listener, [bool useCapture]) native;
@DomName('EventSource.onerror')
@DocsEditable
Stream<Event> get onError => errorEvent.forTarget(this);
@DomName('EventSource.onmessage')
@DocsEditable
Stream<MessageEvent> get onMessage => messageEvent.forTarget(this);
@DomName('EventSource.onopen')
@DocsEditable
Stream<Event> get onOpen => openEvent.forTarget(this);
}
Extends
Interceptor > EventTarget > EventSource
Static Properties
const int CLOSED #
static const int CLOSED = 2
const EventStreamProvider<Event> errorEvent #
static const EventStreamProvider<Event> errorEvent = const EventStreamProvider<Event>('error')
const EventStreamProvider<MessageEvent> messageEvent #
static const EventStreamProvider<MessageEvent> messageEvent = const EventStreamProvider<MessageEvent>('message')
const EventStreamProvider<Event> openEvent #
static const EventStreamProvider<Event> openEvent = const EventStreamProvider<Event>('open')
Constructors
Properties
final int hashCode #
Get a hash code for this object.
All objects have hash codes. Hash codes are guaranteed to be the
same for objects that are equal when compared using the equality
operator ==. Other than that there are no guarantees about
the hash codes. They will not be consistent between runs and
there are no distribution guarantees.
If a subclass overrides hashCode it should override the
equality operator as well to maintain consistency.
int get hashCode => Primitives.objectHashCode(this);
final Events on #
This is an ease-of-use accessor for event streams which should only be used when an explicit accessor is not available.
Events get on => new Events(this);
final Stream<Event> onError #
@DomName('EventSource.onerror')
@DocsEditable
Stream<Event> get onError => errorEvent.forTarget(this);
final Stream<MessageEvent> onMessage #
event field arrives.
@DomName('EventSource.onmessage')
@DocsEditable
Stream<MessageEvent> get onMessage => messageEvent.forTarget(this);
final Stream<Event> onOpen #
@DomName('EventSource.onopen')
@DocsEditable
Stream<Event> get onOpen => openEvent.forTarget(this);
Operators
bool operator ==(other) #
The equality operator.
The default behavior for all Objects is to return true if and
only if this and
other are the same object.
If a subclass overrides the equality operator it should override
the hashCode method as well to maintain consistency.
bool operator ==(other) => identical(this, other);
Methods
void close() #
readyState attribute to CLOSED. If the connection is already closed, the method does nothing.
@DomName('EventSource.close')
@DocsEditable
void close() native;
bool dispatchEvent(Event evt) #
@DomName('EventSource.dispatchEvent')
@DocsEditable
bool dispatchEvent(Event evt) native;
dynamic noSuchMethod(Invocation invocation) #
noSuchMethod is invoked when users invoke a non-existant method
on an object. The name of the method and the arguments of the
invocation are passed to noSuchMethod in an Invocation.
If noSuchMethod returns a value, that value becomes the result of
the original invocation.
The default behavior of noSuchMethod is to throw a
noSuchMethodError.
dynamic noSuchMethod(Invocation invocation) {
throw new NoSuchMethodError(
this,
_symbolToString(invocation.memberName),
invocation.positionalArguments,
_symbolMapToStringMap(invocation.namedArguments));
}
String toString() #
Returns a string representation of this object.
String toString() => Primitives.objectToString(this);
void $dom_addEventListener(String type, EventListener listener, [bool useCapture]) #
@JSName('addEventListener')
@DomName('EventSource.addEventListener')
@DocsEditable
void $dom_addEventListener(String type, EventListener listener, [bool useCapture]) native;
void $dom_removeEventListener(String type, EventListener listener, [bool useCapture]) #
@JSName('removeEventListener')
@DomName('EventSource.removeEventListener')
@DocsEditable
void $dom_removeEventListener(String type, EventListener listener, [bool useCapture]) native;
This page includes content from the
Mozilla Foundation that is graciously
licensed under a
Creative Commons: Attribution-Sharealike license.
Mozilla has no other association with Dart or dartlang.org. We
encourage you to improve the web by
contributing to
The Mozilla Developer Network.