简体   繁体   English

为什么网络工作者不工作?

[英]Why don't web workers work?

So, I'm trying to use a web worker in my project to run a long-running process that is currently tying up the UI.因此,我试图在我的项目中使用 Web Worker 来运行一个长期运行的进程,该进程当前正在占用 UI。 I've been to I don't know how many sites trying to get a worker to work, but to no avail.我去过我不知道有多少网站试图让工人上班,但都无济于事。

All of my javascript is kept in separate files and referenced in the HTML file.我所有的 javascript 都保存在单独的文件中,并在 HTML 文件中引用。 As a test to get my feet wet, I created a test.js file and put the following code in it:作为让我的脚弄湿的测试,我创建了一个 test.js 文件并将以下代码放入其中:

self.addEventListener('message', function(e) { 
self.postMessage('return');},false);

Then, in the UI page's javascript file I placed this code in a function triggered by a button click event:然后,在 UI 页面的 javascript 文件中,我将此代码放在由按钮单击事件触发的函数中:

var w = new Worker('test.js');
w.addEventListener('message',function(e){
alert(e.data);},false);
w.postMessage('hi');

The code is derived from:代码来源于:

html5rocks.com/en/tutorials/workers/basics html5rocks.com/en/tutorials/workers/basics

Other websites I visited provided similar instructions on how to set up a worker.我访问过的其他网站提供了关于如何设置工人的类似说明。

For the life of me, I cannot get this to work.对于我的生活,我无法让它发挥作用。 When I execute it does absolutely nothing and I seemingly get no errors.当我执行它时,它完全没有任何作用,而且我似乎没有收到任何错误。 Stepping through the code, it appears to create the worker, but I don't see any evidence of the event listener being created and the 'postMessage' event doesn't do anything.单步执行代码,它似乎创建了工作程序,但我没有看到任何正在创建事件侦听器的证据,并且 'postMessage' 事件不执行任何操作。 I've tried IE11 and Chrome with the same results.我试过 IE11 和 Chrome,结果相同。

In my research, I came across a part of Chrome's developer tools that revealed the test.js file couldn't be found.在我的研究中,我发现 Chrome 的开发者工具的一部分显示找不到 test.js 文件。 Yet, the file is in the same folder as the page's js file.但是,该文件与页面的 js 文件位于同一文件夹中。 So, I tried adding in the relative directory information as I do in the page's HTML section.因此,我尝试像在页面的 HTML 部分那样添加相关目录信息。 That didn't work either.那也没有用。

I then found claims that for security reasons you couldn't have one js file reference another js in the code.然后我发现声称出于安全原因你不能让一个 js 文件在代码中引用另一个 js。 It's unclear whether this is a Chrome-only feature or part of some spec.目前尚不清楚这是 Chrome 独有的功能还是某些规范的一部分。

So, now I'm in a quandary.所以,现在我陷入了困境。 The worker requires a reference to a separate js file for the code to be executed, yet, the browser isn't allowed to reference another file? worker 需要引用一个单独的 js 文件才能执行代码,但是,浏览器不允许引用另一个文件? How is the worker supposed to work if you aren't allowed to do what it requires to work?如果你不被允许做它需要的工作,工人应该如何工作?

To now, I've successfully pissed away two days trying to get this one seemingly simple function to work.到目前为止,我已经成功地激怒了两天,试图让这个看似简单的功能发挥作用。 To say I'm mildly frustrated would be an understatement.说我有点沮丧是轻描淡写的。 Being a fairly novice programmer and not understanding every last little nuance about web programming I'm clearly missing a key part of this whole thing.作为一个相当新手的程序员,并且不了解有关 Web 编程的每一个细微差别,我显然错过了整个事情的关键部分。

How the heck is one supposed to make web workers work?到底应该如何让网络工作者工作?

Turns out browsers won't allow local files to be fetched via javascript.原来浏览器不允许通过 javascript 获取本地文件。 Because that means a website can read your personal files!因为这意味着网站可以读取您的个人文件! So you need to develop and test your project using a web server.因此,您需要使用 Web 服务器来开发和测试您的项目。 The easiest way to do this for me was to install:对我来说最简单的方法是安装:

docker-compose

and make sure it works.并确保它有效。 Then create a file named:然后创建一个名为:

docker-compose.yml

inside root folder of my project with index.html file.在我的项目的根文件夹中,带有 index.html 文件。 Then put this inside the docker-compose.yml file:然后把它放在 docker-compose.yml 文件中:

version: '3'
services:

  nginx:
    image: nginx:alpine
    volumes:
      - .:/usr/share/nginx/html
    ports:
      - "80:80"

Then inside the root folder of my project run:然后在我的项目的根文件夹中运行:

docker-compose up

And then in the browser go to: http://localhost/ And it worked!然后在浏览器中转到: http://localhost/并且它起作用了!

I appear to have found a solution, though it escapes me why.我似乎已经找到了解决方案,尽管我不知道为什么。

If I use:如果我使用:

 var w = new Worker('js\test.js');

the worker doesn't work.工人不工作。

But, if I use:但是,如果我使用:

var w = new Worker('js/test.js');

the worker does work.工人确实工作。

I characteristically use the back slash throughout the project to delineate paths without issue.我的特点是在整个项目中使用反斜杠来划定路径而不会出现问题。 Why the forward slash must be used to set the worker's file location is a mystery.为什么必须使用正斜杠来设置工人的文件位置是一个谜。 I have seen nothing in any documentation that even remotely addresses that tiny, yet seemingly critical detail.我在任何文档中都没有看到任何可以远程解决这个微小但看似关键的细节的内容。

Thank you, Mr. Starke, for your help!谢谢你,斯塔克先生,谢谢你的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM