我正在尝试设置this example并使其工作,但是在最简单的一个脚本文件和一个工作文件上,它永远不会从服务工作者向客户端发送消息。可能是什么问题?
Google Chrome 版本64.0.3282.167(官方构建)(64位)
navigator.serviceWorker.addEventListener('message', event => {
console.log(event.data.msg, event.data.url);
});
navigator.serviceWorker.register("sw.js")
.then(function (registration) {
console.log("ServiceWorker registration successful with scope: ", registration.scope);
})
.catch(function (error) {
console.error("Service Worker Error", error);
});
addEventListener('install', event => {
event.waitUntil(self.skipWaiting());
});
addEventListener('activate', event => {
event.waitUntil(self.clients.claim());
});
addEventListener('fetch', event => {
event.waitUntil(async function() {
// Exit early if we don't have access to the client.
// Eg, if it's cross-origin.
if (!event.clientId) return;
// Get the client.
const client = await clients.get(event.clientId);
// Exit early if we don't get the client.
// Eg, if it closed.
if (!client) return;
// Send a message to the client.
client.postMessage({
msg: "Hey I just got a fetch from you!",
url: event.request.url
});
}());
});
我在每个地方都console.log
,并且正在调用方法client.postMessage
。
这适用于 Mozilla Firefox Quantum 58.0.2(64位)
答案 0 :(得分:0)
必须首先获取客户端对象。将此添加到您的>>> from collections import deque
>>> deque(range(100000),5)
deque([99995, 99996, 99997, 99998, 99999], maxlen=5)
侦听器中。
fetch