Dart API Referencedart:htmlEventSource

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 #

The connection is not being established, has been closed or there was a fatal error.
static const int CLOSED = 2

const int CONNECTING #

The connection is being established.
static const int CONNECTING = 0

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 int OPEN #

The connection is open and dispatching events.
static const int OPEN = 1

const EventStreamProvider<Event> openEvent #

static const EventStreamProvider<Event> openEvent = const EventStreamProvider<Event>('open')

Constructors

factory EventSource(String title, {withCredentials: false}) #

factory EventSource(String title, {withCredentials: false}) {
 var parsedOptions = {
   'withCredentials': withCredentials,
 };
 return EventSource._factoryEventSource(title, parsedOptions);
}

Properties

final int hashCode #

inherited from Interceptor

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.

docs inherited from Object
int get hashCode => Primitives.objectHashCode(this);

final Events on #

inherited from EventTarget

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 #

A JavaScript function to call when an error occurs.
@DomName('EventSource.onerror')
@DocsEditable
Stream<Event> get onError => errorEvent.forTarget(this);

final Stream<MessageEvent> onMessage #

A JavaScript function to call when an a message without an event field arrives.
@DomName('EventSource.onmessage')
@DocsEditable
Stream<MessageEvent> get onMessage => messageEvent.forTarget(this);

final Stream<Event> onOpen #

A JavaScript function to call when the connection has opened.
@DomName('EventSource.onopen')
@DocsEditable
Stream<Event> get onOpen => openEvent.forTarget(this);

final int readyState #

The state of the connection, must be one of CONNECTING, OPEN, or CLOSED. Read only.
final int readyState

final Type runtimeType #

inherited from Interceptor

A representation of the runtime type of the object.

docs inherited from Object
Type get runtimeType => getRuntimeType(this);

final String url #

Read only.
final String url

final bool withCredentials #

final bool withCredentials

Operators

bool operator ==(other) #

inherited from Interceptor

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.

docs inherited from Object
bool operator ==(other) => identical(this, other);

Methods

void close() #

Closes the connection, if any, and sets the 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) #

inherited from Interceptor

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.

docs inherited from Object
dynamic noSuchMethod(Invocation invocation) {
 throw new NoSuchMethodError(
     this,
     _symbolToString(invocation.memberName),
     invocation.positionalArguments,
     _symbolMapToStringMap(invocation.namedArguments));
}

String toString() #

inherited from Interceptor

Returns a string representation of this object.

docs inherited from 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.