HTML5:具有多个连接的共享Web工作者 [英] HTML5: shared web worker with multiple connections

查看:136
本文介绍了HTML5:具有多个连接的共享Web工作者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的理解,HTML5共享Web工作者的最大好处是他们可以在一个单独的执行线程中接受多个连接。

From what I understand, the big benefit of HTML5's shared web workers is that they can accept multiple connections in a single separate thread of execution.

我的问题是:有没有人与SharedWorker建立多个连接,以便与Google Chrome一起作为一个单独的线程?我使用的是最新版本12.0.742.112。

My question is: has anyone gotten multiple connections with a SharedWorker to work as a single thread with Google Chrome? I'm using latest version 12.0.742.112.

演示: http://demos.zulius.com/html5/sharedworker

来源(如果演示已关闭): index.html sharedworker.js

Source (in case demo is down): index.html, sharedworker.js

该演示建立了2个独立的事件监听器。预期的输出是:

The demo establishes 2 separate event listeners. The expected output is:

foo got message: Hello World! You are connection #1
bar got message: Hello World! You are connection #2

在演示中,两个事件侦听器都正确触发,但连接计数变量是未在SharedWorker脚本中维护。这让我相信每个与SharedWorker的连接都在一个单独的线程中执行。

In the demo, both event listeners fire correctly, but the connection count variable is not maintained in the SharedWorker script. This leads me to believe each connection to the SharedWorker is executing in a separate thread.

我做错了什么?或者Chrome对SharedWorker的支持不是很好吗?

Am I doing something wrong? Or is Chrome support for SharedWorker not quite there?

更新:该演示现在正常运行。

UPDATE: the demo works now.

推荐答案

你有2个工作者的监听器,但你只启动了工人一次,所以它是由1个所有者而不是2个所有者共享的1个工人。增加侦听器的数量不会影响所有权。

You have 2 listeners to the Worker but you only start the Worker for once, so it's 1 Worker shared by 1 owner instead of 2 owners. Increasing the number of listeners doesn't affect the ownership.

您可以在此处看到示例:
http://weblog.bocoup.com/javascript-web-workers-chrome-5-supports-new-sharedworker

You can see the example here: http://weblog.bocoup.com/javascript-web-workers-chrome-5-supports-new-sharedworker

它有2个框架,一个包含iframe,另一个包含在iframe内。它们都调用了Worker的 start 方法,因此它由2个所有者共享。由于 start 方法被调用两次, onconnect 事件应该被触发两次,从而使 connection.count 等于2.

It has 2 frames, one containing the iframe and one inside the iframe. They both call the start method of the Worker so it's shared by 2 owners. Since the start method is called twice, the onconnect event should be fired twice, thus making connection.count equal 2.

这篇关于HTML5:具有多个连接的共享Web工作者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆