EventSource

この記事は技術レビューを必要としています。

この記事はまだ日本語に翻訳されていません。MDN の翻訳はボランティアによって行われています。是非 MDN に登録し、私たちの力になって下さい。

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.

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.

Method overview

void close();
void init(in nsIPrincipal principal, in nsIScriptContext scriptContext, in nsPIDOMWindow ownerWindow, in DOMString url); Native code only!

Attributes

Attribute Type Description
onerror nsIDOMEventListener A JavaScript function to call when an error occurs.
onmessage nsIDOMEventListener A JavaScript function to call when an a message without an event field arrives.
onopen nsIDOMEventListener A JavaScript function to call when the connection has opened.
readyState long The state of the connection, must be one of CONNECTING, OPEN, or CLOSED. Read only.
url DOMString Read only.

In addition to these exposed attributes, there are two internal attributes that are not exposed directly:

reconnection time
This is a time, in milliseconds, used to determine how long to wait after a failed attempt to connect before trying again.
last event ID string
This is initially the empty string; the server can send a message with the field name id to set this value.

Constants

Constant Value Description
CONNECTING 0 The connection is being established.
OPEN 1 The connection is open and dispatching events.
CLOSED 2 The connection is not being established, has been closed or there was a fatal error.

Methods

close()

Closes the connection, if any, and sets the readyState attribute to CLOSED. If the connection is already closed, the method does nothing.

void close();
Parameters

None.

Native code only!

init

Initializes the object for use from C++ code with the principal, script context, and owner window that should be used.

void init(
  in nsIPrincipal principal,
  in nsIScriptContext scriptContext,
  in nsPIDOMWindow ownerWindow,
  in DOMString url
);
Parameters
principal
The principal to use for the request. This must not be null.
scriptContext
The script context to use for the request. May be null.
ownerWindow
The associated window for the request. May be null.
url
The EventSource's URL. This must not be empty.

See also

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
EventSource support 9 6.0 (6.0) ? 11 5
Cross-Origin Resource Sharing
support
26 11.0 (11.0) ? 12 ?
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
EventSource support 4.4 ? ? ? ?

 

Document Tags and Contributors

Contributors to this page: Sheppy, Psz, Kornel, coreyauger, Yaffle, kscarfone, SaschaNaz, teoli, iamanupmenon
最終更新者: coreyauger,