问题
I can't seem to get my cordova app to execute even a simple web worker, even though the same web worker works in the Chrome browser on the same device.
(Android device, Google Pixel 3, running Android 9, latest patch level. The app was built in PhoneGap build with cli-8.0.0
)
Setup in the main thread:
const worker = new Worker('/fake.worker.js');
worker.postMessage(1337);
worker.addEventListener('message', event => console.log("[myWorker] received event from myWorker:", event));
And the /fake.worker.js
file is just this, no transpilation:
/* eslint-disable no-restricted-globals */
console.log("fake.worker booted");
self.addEventListener("message", startCounter);
function startCounter(event) {
console.log(event.data, self)
let initial = event.data;
console.log("fake.worker counter started:", initial);
setInterval(() => this.postMessage(initial++), 1000);
}
Bottom line, the console never shows anything from the worker, either from the main thread or from the worker thread's console.log
statements.
While attached to the device and remote debugging, I can see that ('Worker' in window) === true
and that the Network tab of the remote console that the fake.worker.js
file is indeed loaded. (Serving the file via ngrok
from my development laptop - after connecting to the remote console, I changed the location of the web view with window.location.href = 'https://....ngrok...'
.)
Note: I also tried including the Web Worker via an inline blob (worker-loader
for webpack automated this for me, but the concept is shown at https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers#Embedded_workers) - and even when "embedded", the worker never booted.
来源:https://stackoverflow.com/questions/55130960/web-worker-in-cordova-is-it-supported