标签: workbox-window

如何从 Service Worker 向 Workbox 类实例的“消息”事件发送消息?

我的 window.js 中有这个...

const wb = new Workbox('sw.js');

wb.messageSW({type:'START'});

wb.addEventListener('message', e=>{
   console.log(e);
});
Run Code Online (Sandbox Code Playgroud)

...在我的 sw.js 中,我有...

self.addEventListener('message', (e)=>{
   if (e.data) {
       switch(e.data.type) {
           case 'START':
               //do some processing here...
               //...then how do I send a message to the client here so...
               //...that it will be received by the wb.addEventListener('message',... in window.js?
           break;
       }
   }
});
Run Code Online (Sandbox Code Playgroud)

我尝试通过 e.ports[0] 中的 MessagePort.postMessage() 发送,但它不起作用。我觉得这是一些基本的东西,特别是使用 Workbox 类,但我就是无法让它工作。

我的最后一招是使用 BroadcastChannel (带有填充),但我首先尝试这个,因为它可能不需要任何填充即可工作。

workbox workbox-window

5
推荐指数
1
解决办法
2654
查看次数

标签 统计

workbox ×1

workbox-window ×1