嵌套共享工作者

发布于 2024-10-30 10:41:13 字数 541 浏览 0 评论 0 原文

是否可以(即使不明智)在另一个 SharedWorker 的处理程序中实例化一个 SharedWorker ?

// Code in sharedworker.js
onconnect = function(e) {
  var port = e.ports[0];
  port.onmessage = function(e) {
    port.postMessage("Hello"); // This is sent.
    var worker = new SharedWorker("worker.js");
    post.postMessage("Goodbye"); // In my tests, this is not sent.
  }
}

// Code in main.js
var worker = new SharedWorker('sharedworker.js');
worker.port.onmessage = function(e) {
  window.console.log(e.data);
}
worker.port.postMessage("Start");

Is it possible (even if unwise) to instantiate a SharedWorker inside the handler for another SharedWorker?

// Code in sharedworker.js
onconnect = function(e) {
  var port = e.ports[0];
  port.onmessage = function(e) {
    port.postMessage("Hello"); // This is sent.
    var worker = new SharedWorker("worker.js");
    post.postMessage("Goodbye"); // In my tests, this is not sent.
  }
}

// Code in main.js
var worker = new SharedWorker('sharedworker.js');
worker.port.onmessage = function(e) {
  window.console.log(e.data);
}
worker.port.postMessage("Start");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

居里长安 2024-11-06 10:41:13

不,SharedWorker< /code> 接口 仅在Window 上下文中公开。您无法直接从另一个 SharedWorker 内部或从专用 Worker 内部创建 SharedWorker

[Exposed=Window]
interface SharedWorker : EventTarget {
  constructor(USVString scriptURL, optional (DOMString or WorkerOptions) options = {});

  readonly attribute MessagePort port;
};
SharedWorker includes AbstractWorker;

但是,您可以做的是从同一 Window 上下文启动两个 SharedWorker 实例,并从 MessagePort ="https://developer.mozilla.org/en-US/docs/Web/API/MessageChannel" rel="nofollow noreferrer">MessageChannel 对象。这样,您的两个 SharedWorker 实例将能够直接相互通信,就像一个实例化了另一个实例一样:

在您的窗口脚本中,您将看到类似以下内容

const a = new SharedWorker("worker-a.js");
const b = new SharedWorker("worker-b.js");
const { port1, port2 } = new MessageChannel();
a.port.postMessage("", [port1]);
b.port.postMessage("", [port2]);

:脚本:

onconnect = (e) => {
  const winPort = e.ports[0];
  winPort.onmessage = (e) => {
    const workerPort = e.ports[0];
    workerPort.onmessage = (evt) => {
      // do something with messages received from the other SharedWorker
    }
    workerPort.postMessage(someData, transferables);
  };
};

不幸的是,StackSnippets iframe 不允许创建 SharedWorker,所以我必须外包 Glitch 项目中的“nofollow noreferrer">现场演示 。

No, the SharedWorker interface is only exposed in Window contexts. You can't create a SharedWorker directly from inside another SharedWorker nor from inside a dedicated Worker.

[Exposed=Window]
interface SharedWorker : EventTarget {
  constructor(USVString scriptURL, optional (DOMString or WorkerOptions) options = {});

  readonly attribute MessagePort port;
};
SharedWorker includes AbstractWorker;

What you can do however is to start both SharedWorker instances from the same Window context and pass to both of them a MessagePort from a MessageChannel object. This way, both your SharedWorker instances will be able to communicate one with each other directly, just like if one did instantiate the other:

In your window's script you'd then have something like:

const a = new SharedWorker("worker-a.js");
const b = new SharedWorker("worker-b.js");
const { port1, port2 } = new MessageChannel();
a.port.postMessage("", [port1]);
b.port.postMessage("", [port2]);

And in you worker scripts:

onconnect = (e) => {
  const winPort = e.ports[0];
  winPort.onmessage = (e) => {
    const workerPort = e.ports[0];
    workerPort.onmessage = (evt) => {
      // do something with messages received from the other SharedWorker
    }
    workerPort.postMessage(someData, transferables);
  };
};

Unfortunately, StackSnippets iframes don't allow to create a SharedWorker, so I have to outsource the live demo into a Glitch project.

挽容 2024-11-06 10:41:13

应该是 port.postMessage("Goodbye"); 而不是 post.postMessage("Goodbye"); 吗?

Should that be port.postMessage("Goodbye"); instead of post.postMessage("Goodbye");?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文