--- title: MessageEvent slug: Web/API/MessageEvent translation_of: Web/API/MessageEvent ---
La interface MessageEvent
representa un mensaje recibido por un objeto de destino.
Este es usado para representar mensajes en :
onmessage
de la interface WebSocket ).La acción desencadenada por este evento es definida en una función establecida como el controlador de eventos para el evento pertinente {{event("message")}} ( es decir : Usando un manejador de onmessage
como se lista arriba).
{{AvailableInWorkers}}
MessageEvent
.Esta interface tambien herada propiedadesde desde su padre {{domxref("Evento")}}.
MessageEventSource
(El cual puede ser un {{domxref("WindowProxy")}}, {{domxref("MessagePort")}}, or {{domxref("ServiceWorker")}} object) es ima representación del mensaje emitido.Esta interface tambien herada propiedadesde desde su padre, {{domxref("Evento")}}.
En nuestro Ejemplo basico de trabajador compartido(Ejecutar trabajador compartdo ), Tenemos dos paginas HTML, cada una de las cuales usa algo de JavaScript para mejorar un calculo simple. Los diferentes scripts estan usando el mismo archivo de trabajo para mejorar el calculo — Ambos pueden accederlo, Incluso si sus paginas esta corriendo n diferentes ventanas.
The following code snippet shows creation of a SharedWorker
object using the {{domxref("SharedWorker.SharedWorker", "SharedWorker()")}} constructor. Both scripts contain this:
var myWorker = new SharedWorker('worker.js');
Both scripts then access the worker through a {{domxref("MessagePort")}} object created using the {{domxref("SharedWorker.port")}} property. If the onmessage event is attached using addEventListener, the port is manually started using its start()
method:
myWorker.port.start();
When the port is started, both scripts post messages to the worker and handle messages sent from it using port.postMessage()
and port.onmessage
, respectively:
first.onchange = function() { myWorker.port.postMessage([first.value,second.value]); console.log('Message posted to worker'); } second.onchange = function() { myWorker.port.postMessage([first.value,second.value]); console.log('Message posted to worker'); } myWorker.port.onmessage = function(e) { result1.textContent = e.data; console.log('Message received from worker'); }
Inside the worker we use the {{domxref("SharedWorkerGlobalScope.onconnect")}} handler to connect to the same port discussed above. The ports associated with that worker are accessible in the {{event("connect")}} event's ports
property — we then use {{domxref("MessagePort")}} start()
method to start the port, and the onmessage
handler to deal with messages sent from the main threads.
onconnect = function(e) { var port = e.ports[0]; port.addEventListener('message', function(e) { var workerResult = 'Result: ' + (e.data[0] * e.data[1]); port.postMessage(workerResult); }); port.start(); // Required when using addEventListener. Otherwise called implicitly by onmessage setter. }
Especificación | Estado | Comentario |
---|---|---|
{{SpecName('HTML WHATWG', "#messageevent", "MessageEvent")}} | {{Spec2('HTML WHATWG')}} |
{{CompatibilityTable}}
Caracteristica | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 1 | {{CompatVersionUnknown}} | {{CompatGeckoDesktop("2.0")}} | 9 | {{CompatUnknown}} | {{CompatSafari('10.0+')}} |
origin as {{domxref("USVString")}} and source as MessageEventSource |
{{CompatVersionUnknown}} | {{CompatUnknown}} | {{CompatGeckoDesktop("55.0")}} | {{CompatNo}} | {{CompatUnknown}} | {{CompatUnknown}} |
Feature | Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | {{CompatUnknown}} | {{CompatVersionUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | 3.0+ |
origin as {{domxref("USVString")}} and source as MessageEventSource |
{{CompatUnknown}} | {{CompatUnknown}} | {{CompatGeckoMobile("55.0")}} | {{CompatNo}} | {{CompatUnknown}} | {{CompatUnknown}} |