Movatterモバイル変換


[0]ホーム

URL:


  1. Tecnología web para desarrolladores
  2. API web
  3. EventSource

Esta página ha sido traducida del inglés por la comunidad.Aprende más y únete a la comunidad de MDN Web Docs.

View in EnglishAlways switch to English

EventSource

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since enero de 2020.

* Some parts of this feature may have varying levels of support.

La interfazEventSource se utiliza para recibir eventos server-side. Se realiza la conexión a un servidor sobre HTTP y se reciben eventos en formatotext/event-stream sin tener que cerrar la conexión.

Constructor

EventSource()

Crea un nuevoEventSource a partiendo de un valorUSVString.

Propiedades

Esta interfaz también heredará propiedades de su antecesor,EventTarget.

EventSource.readyStateRead only

Un número representando el estado de la conexión. Los valores posibles son CONECTANDO (0), ABIERTO (1), o CERRADO (2).

EventSource.urlRead only

Un valorDOMString representando la URL de la fuente.

EventSource.withCredentialsRead only

Un valorBoolean indicando si el objectoEventSource ha sido instanciado con credeciales CORS disponibles (true) o no (false, valor por defecto).

Manejadores de Eventos

EventSource.onerror

En unevent handler que se invoca cuando ocurre un error y se envía el eventoerror a través del objetoEventSource.

EventSource.onmessage

Es unevent handler que se invoca cuando se recibe un eventomessage, que indica que se ha enviado un mensaje desde la fuente.

EventSource.onopen

Es unevent handler que se invoca cuando se recibe un eventoopen, que sucede en el momento que la conexión se abre.

Métodos

Esta interfaz también heredará métodos de su antecesor,EventTarget.

EventSource.close()

Cierra la conexión, si ésta existe, y asigna el valor CLOSED al atributoreadyState. Si la conexión ya estaba cerrada, este método no hace nada.

Ejemplos

js
var evtSource = new EventSource("sse.php");var eventList = document.querySelector("ul");evtSource.onmessage = function (e) {  var newElement = document.createElement("li");  newElement.textContent = "message: " + e.data;  eventList.appendChild(newElement);};

Nota:Está disponible un ejemplo completo en GitHub — verSimple SSE demo using PHP.

Especificaciones

Specification
HTML
# the-eventsource-interface

Compatibilidad con navegadores

Ver también

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2026 Movatter.jp