简体   繁体   English

如何通过HTTPS加载Web Worker脚本?

[英]How do I load a Web Worker script over HTTPS?

I am attempting to use a Web Worker to offload some CPU intensive calculations into a separate thread. 我试图使用Web Worker将一些CPU密集型计算卸载到一个单独的线程中。 For a little context, I am taking an audio stream from getUserMedia and saving it into a file to be uploaded to my service after it is complete. 对于一个小环境,我从getUserMedia获取音频流并将其保存到文件中,以便在完成后上传到我的服务。 I am able to retrieve the stream from the user and play it back via the WebAudio API and through an HTML5 player, but now I need to take the next step of saving it into a file. 我能够从用户检索流并通过WebAudio API和HTML5播放器播放,但现在我需要采取下一步将其保存到文件中。

The problem : 问题

My main service is running over an HTTPS connection, since it is restricted to signed in users only. 我的主要服务是通过HTTPS连接运行,因为它仅限于已登录的用户。 I have a worker script that does what I need it to, and I am attempting to load the script in via a relative path into my worker. 我有一个工作脚本,可以完成我需要的工作,我试图通过相对路径将脚本加载到我的工作人员中。 I am receiving the following error 我收到以下错误

Mixed Content: The page at 'https://someurl.com:1081/some/path' was loaded over HTTPS, 
but requested an insecure Worker script 
'http://someurl.com/some/path/lib/assets/javascripts/worker.js'. 
This request has been blocked; the content must be served over HTTPS.

I figured it was because I was using a relative path in my code like so: 我想是因为我在代码中使用了相对路径,如下所示:

worker = new Worker('lib/assets/javascripts/worker.js');

I wanted to rule this out so I made the following change: 我想排除这一点,所以我做了以下改动:

worker = new Worker('https://someurl.com:1081/some/path/lib/assets/javascripts/worker.js');

This did not solve my error. 这并没有解决我的错误。 It appears that the Worker is loading my script via HTTP no matter what url location I attempt to use. 似乎Worker正在通过HTTP加载我的脚本,无论我尝试使用哪个url位置。 I couldn't find any reference on how to use the Web Worker via HTTPS, so I am hoping someone can provide some insight. 我找不到任何关于如何通过HTTPS使用Web Worker的参考,所以我希望有人可以提供一些见解。

Possible Solution 可能解决方案

I do want you to know there is a possible solution, but it seems a bit hacky to me. 我确实希望你知道有一个可能的解决方案,但对我来说这似乎有些笨拙 I can load my worker script up as a Blob and pass that directly into the Worker . 我可以将我的工作脚本加载为Blob并将其直接传递给Worker If this is the only solution, I can make it work. 如果这是唯一的解决方案,我可以使它工作。 But I was hoping to find a way to make the script load via HTTPS. 但我希望找到一种方法来通过HTTPS加载脚本。

Have you tried 你有没有尝试过

//someurl.com:1081/some/path/lib/assets/javascripts/worker.js 

instead of 代替

https://someurl.com:1081/some/path/lib/assets/javascripts/worker.js

Just something I found here, Deezer content is served over HTTP 我在这里找到的东西, Deezer内容是通过HTTP提供的

I solved this. 我解决了这个问题 The error itself was misleading and caused me to go down a rabbit hole looking for the solution. 错误本身是误导性的,导致我走下一个寻找解决方案的兔子洞。

The issue here actually stems from the way I have this service configured. 这里的问题实际上源于我配置此服务的方式。 The service that starts the web worker is actually proxied behind another service, and all requests go through the parent service. 启动Web worker的服务实际上是在另一个服务后面代理,所有请求都通过父服务。 This works great for most requests, but was causing an error in this case. 这适用于大多数请求,但在这种情况下导致错误。 Instead of forwarding the request on this port to my app, the web worker was attempting to download the worker script from the parent service itself. Web工作者试图从父服务本身下载工作脚本,而不是将此端口上的请求转发到我的应用程序。 This means the error stemmed from the fact that the script wasn't found, not that the protocol was incorrect. 这意味着错误源于未找到脚本的事实,而不是协议不正确。

To solve this, I had to pass in a localized script location from Rails using its asset pipeline. 为了解决这个问题,我不得不使用其资产管道从Rails传递一个本地化的脚本位置。 This allowed the worker to grab the script and actually work. 这允许工作人员抓住脚本并实际工作。

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

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