ExtendableMessageEvent

ExtendableMessageEvent interface of the Service Worker API represents the event object of a message event fired on a service worker (when a message is received on the ServiceWorkerGlobalScope from another context) — extends the lifetime of such events.

This interface inherits from the ExtendableEvent 接口。

构造函数

ExtendableMessageEvent()
创建新的 ExtendableMessageEvent object instance.

特性

Inherits properties from its parent, ExtendableEvent .

ExtendableMessageEvent.data 只读
Returns the event's data. It can be any data type.
ExtendableMessageEvent.origin 只读
Returns the origin of the Client that sent the message.
ExtendableMessageEvent.lastEventId 只读
Represents, in server-sent events , the last event ID of the event source. This is an empty string.
ExtendableMessageEvent.source 只读
Returns a reference to the Client object that sent the message.
ExtendableMessageEvent.ports 只读
Returns the array containing the MessagePort objects representing the ports of the associated message channel.

方法

Inherits methods from its parent, ExtendableEvent .

范例

In the below example a page gets a handle to the ServiceWorker object via ServiceWorkerRegistration.active , and then calls its postMessage() 函数。

// in the page being controlled
if (navigator.serviceWorker) {
  navigator.serviceWorker.register('service-worker.js');
  navigator.serviceWorker.addEventListener('message', event => {
    // event is a MessageEvent object
    console.log(`The service worker sent me a message: ${event.data}`);
  });
  navigator.serviceWorker.ready.then( registration => {
    registration.active.postMessage("Hi service worker");
  });
}
					

The service worker can receive the message by listening to the message event:

// in the service worker
addEventListener('message', event => {
  // event is an ExtendableMessageEvent object
  console.log(`The client sent me a message: ${event.data}`);
  event.source.postMessage("Hi client");
});
					

规范

规范 状态 Comment
服务工作者
The definition of 'ExtendableMessageEvent' in that specification.
工作草案 初始定义。

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
ExtendableMessageEvent
Chrome Yes Edge ≤18 Firefox 45
注意事项
45
注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera ? Safari 不支持 No WebView Android 不支持 No Chrome Android Yes Firefox Android 45 Opera Android ? Safari iOS 不支持 No Samsung Internet Android Yes
ExtendableMessageEvent() 构造函数
Chrome ? Edge ? Firefox 45
注意事项
45
注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera ? Safari 不支持 No WebView Android 不支持 No Chrome Android ? Firefox Android 45 Opera Android ? Safari iOS 不支持 No Samsung Internet Android ?
data
Chrome 51 Edge ≤79 Firefox 45
注意事项
45
注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera ? Safari 不支持 No WebView Android 不支持 No Chrome Android 51 Firefox Android 45 Opera Android ? Safari iOS 不支持 No Samsung Internet Android 5.0
lastEventId
Chrome ? Edge 17 Firefox 45
注意事项
45
注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera ? Safari 不支持 No WebView Android 不支持 No Chrome Android ? Firefox Android 45 Opera Android ? Safari iOS 不支持 No Samsung Internet Android ?
origin
Chrome ? Edge ? Firefox 不支持 No IE 不支持 No Opera ? Safari 不支持 No WebView Android 不支持 No Chrome Android ? Firefox Android 不支持 No Opera Android ? Safari iOS 不支持 No Samsung Internet Android ?
ports
Chrome ? Edge ? Firefox 45
注意事项
45
注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera ? Safari 不支持 No WebView Android 不支持 No Chrome Android ? Firefox Android 45 Opera Android ? Safari iOS 不支持 No Samsung Internet Android ?
source
Chrome ? Edge ? Firefox 45
注意事项
45
注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera ? Safari 不支持 No WebView Android 不支持 No Chrome Android ? Firefox Android 45 Opera Android ? Safari iOS 不支持 No Samsung Internet Android ?

图例

完整支持
完整支持
不支持
不支持
兼容性未知
兼容性未知
实验。期望将来行为有所改变。
实验。期望将来行为有所改变。
见实现注意事项。

另请参阅