【问题标题】:Access scope with multithread.js使用 multithread.js 访问范围
【发布时间】:2016-06-12 19:53:59
【问题描述】:

我尝试生成一个 RSA 密钥,由于这需要一些时间,我想使用 multithread.js 来生成密钥。

我的代码:

<script src="../js/multithread.js"></script>
<script src="../js/crypto/cryptico.js"></script> 
<script>
var keyPair;
var MT = new Multithread(2);

MT.process(
            function () {
                var bits = 4096;
                return cryptico.generateRSAKey("passphrase", bits);
            }, function (key) {
                keyPair = key;
                publicKey = cryptico.publicKeyString(keyPair);
            }
)();
</script>

我得到一个

ReferenceError: cryptico is not defined

错误。

我知道 multithread.js 与应用程序的作用域不同。 那么如何访问 cryptico 库或将范围传递给 multithread.js?

【问题讨论】:

    标签: javascript multithreading cryptico


    【解决方案1】:

    我自己找到了解决方案。

    只需在函数中包含importScripts 的必要文件。 Webworker 定义了这个方法。

    像这样:

    MT.process(
            function () {
                importScripts('http://localhost:63342/..../js/crypto/cryptico.js');
                var bits = 4096;
                return cryptico.generateRSAKey("passphrase", bits);
            }, function (key) {
                keyPair = key;
                publicKey = cryptico.publicKeyString(keyPair);
            }
    )();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-23
      • 1970-01-01
      • 1970-01-01
      • 2012-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多