Shared Web Workers 是否在单个页面重新加载、链接导航中持续存在 [英] Do Shared Web Workers persist across a single page reload, link navigation

查看:10
本文介绍了Shared Web Workers 是否在单个页面重新加载、链接导航中持续存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

共享网络工作者旨在允许来自同一站点(来源)共享单个 Web Worker.

Shared Web Workers are designed to allow multiple pages from the same site (origin) to share a single Web Worker.

但是,从规范(或其他教程和有关 Shared Workers 的信息)我不清楚如果您的站点只有一个窗口/选项卡并且您导航到同一站点上的另一个页面,Shared Worker 是否会持续存在.

However, it's not clear to me from the spec (or other tutorials and information on Shared Workers) whether the Shared Worker will persist if you have only one window/tab from the site and you navigate to another page on the same site.

这在来自 Shared Worker 的 WebSocket 连接的情况下最有用,该连接在站点导航时保持连接.例如,假设一个股票行情或聊天区即使在网站被导航时也能持续存在(无需重新连接 WebSocket).

This would be most useful in the case of a WebSocket connection from the Shared Worker that stays connected as the site is navigated. For example, imagine a stock ticker or chat area that would persist (without having to reconnect the WebSocket) even while the site is navigated.

推荐答案

我已经做了一些测试以在实践中找到答案.

I have done some testing to find out the answer to this in practice.

Firefox 尚不支持从 Web Workers 创建 WebSocket 连接:https://bugzilla.mozilla.org/show_bug.cgi?id=504553 因此,在该错误得到解决之前,Firefox 并不相关.

Firefox does not yet support creating WebSocket connections from Web Workers: https://bugzilla.mozilla.org/show_bug.cgi?id=504553 So Firefox is not relevant until that bug is resolved.

IE 10 没有支持共享网络工作者,所以它也不相关.这样就离开了 Chrome.

IE 10 doesn't have support for Shared Web Workers so it's not relevant either. So that leaves Chrome.

这是一个测试共享网络工作者的示例.

Here is an example to test shared web workers.

首先是 HTML:

<!DOCTYPE html>
<html>
<body>
    <a href="shared.html">reload page</a>
    <script>
        var worker = new SharedWorker("shared.js");
        worker.port.addEventListener("message", function(e) {
            console.log("Got message: " + e.data);
        }, false);
        worker.port.start();
        worker.port.postMessage("start");
    </script>
</body>
</html>

然后是shared.js中共享worker本身的实现:

Then the implementation of the shared worker itself in shared.js:

var connections = 0;

self.addEventListener("connect", function(e) {
    var port = e.ports[0];
    connections ++;
    port.addEventListener("message", function(e) {
        if (e.data === "start") {
            var ws = new WebSocket("ws://localhost:6080");
            port.postMessage("started connection: " + connections);
        }
    }, false);
    port.start();
}, false);

Chrome 20 中的测试结果(答案):

Test results in Chrome 20 (the answer):

当页面在两个单独的选项卡中同时加载时,每次重新加载其中一个页面或单击自引用链接时,连接数都会增加.

When the page is loaded simultaneously in two separate tabs, the connection count grows each time one of the pages is reloaded or the self-referential link is clicked.

如果只加载了页面的单个实例,则在重新加载页面或单击链接时连接数永远不会改变.

If only a single instance of the page is loaded then the connection count never changes when the page is reloaded or the link is clicked.

因此,在 Chrome 20 中:Shared Web Workers 不会在页面重新加载和链接导航点击后持续存在.

So, in Chrome 20: Shared Web Workers do not persist across page reloads and link navigation clicks.

这篇关于Shared Web Workers 是否在单个页面重新加载、链接导航中持续存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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