HTML5:具有多个连接的共享 Web Worker
据我了解,HTML5 共享 Web Worker 的一大好处是它们可以在单个单独的执行线程中接受多个连接。
我的问题是:是否有人获得了与 SharedWorker 的多个连接,以作为 Google Chrome 的单个线程工作?我使用的是最新版本 12.0.742.112。
演示:http://demos.zulius.com/html5/sharedworker
源(如果演示是向下):index.html,sharedworker.js
该演示建立了 2 个独立的事件侦听器。预期输出为:
foo got message: Hello World! You are connection #1
bar got message: Hello World! You are connection #2
在演示中,两个事件侦听器均正确触发,但 SharedWorker 脚本中未维护连接计数变量。这让我相信与 SharedWorker 的每个连接都在单独的线程中执行。
我做错了什么吗?或者 Chrome 对 SharedWorker 的支持还不够?
更新:演示现在可以运行了。
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.
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.
Demo: http://demos.zulius.com/html5/sharedworker
Source (in case demo is down): index.html, sharedworker.js
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
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.
Am I doing something wrong? Or is Chrome support for SharedWorker not quite there?
UPDATE: the demo works now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有 2 个 Worker 侦听器,但只启动 Worker 一次,因此它是由 1 个所有者而不是 2 个所有者共享的 1 个 Worker。增加听众数量不会影响所有权。
您可以在此处查看示例:
http://weblog.bocoup.com/javascript-web -workers-chrome-5-supports-new-sharedworker
它有 2 个框架,一个包含 iframe,另一个包含 iframe。他们都调用 Worker 的
start
方法,因此它由 2 个所有者共享。由于start
方法被调用两次,因此onconnect
事件应该被触发两次,从而使connection.count
等于 2。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.
You can see the example here:
http://weblog.bocoup.com/javascript-web-workers-chrome-5-supports-new-sharedworker
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 thestart
method is called twice, theonconnect
event should be fired twice, thus makingconnection.count
equal 2.在共享网络工作者中,上下文一直有效,直到最后一个浏览器会话结束。共享网络工作者可以维护浏览器选项卡周围的上下文。它们使用相同的数据上下文响应请求。
数据上下文的更改将影响所有连接,您可以通过单个上下文更改更新所有连接,您可以维护数据直到最后一个连接结束。您可以在所有视图中维护连接更改。
这是具有多个连接的共享网络工作人员的演示。
http://www.antkorp.in/sharedworkers/
In shared webworkers the context is alive till the last browser session end. shared webworkers can maintain the context around the browser tabs. They respond to the requests with the same context of data.
The change in context of data will affect all connections, the possibilities are you can update all the connections with single context change, you can maintain the data till the last connection end. you can maintain the connection changes in all views.
Here is a demo of Shared web workers with multiple connections.
http://www.antkorp.in/sharedworkers/