共享Web工作人员在单个页面上持续存储重新加载,链接导航 [英] Do Shared Web Workers persist across a single page reload, link navigation

查看:90
本文介绍了共享Web工作人员在单个页面上持续存储重新加载,链接导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

共享Web工作者旨在允许多个页面来自同一个站点(来源)共享一个Web Worker。



但是,我不清楚规范(或其他关于共享工作者的教程和信息)是否如果您只有一个窗口/选项卡,并且您导航到同一站点上的另一个页面,共享工作人员将保持不变。



这对于以下情况最有用:一个来自共享工作者的WebSocket连接,在站点被导航时保持连接状态。例如,想象一个股票代码或聊天区域,即使在网站导航时,它们也会持续存在(不必重新连接WebSocket)。 解决方案

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



Firefox还不支持从Web Workers创建WebSocket连接: https://bugzilla.mozilla.org/show_bug.cgi?id=504553 所以Firefox不相关直到该错误得到解决。



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



以下是测试共享网络工作者的示例。



首先是HTML:

 <!DOCTYPE html> 
< html>
< body>
< a href =shared.html>重新加载页面< / 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

  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(已启动连接:+连接);
}
},false);
port.start();
},false);

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



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



如果只加载了一个页面实例,那么当重新加载页面或点击链接时,连接计数永远不会改变。



因此,在Chrome 20中:共享网络工作者不会在页面重新加载和链接导航点击中保持


Shared Web Workers are designed to allow multiple pages from the same site (origin) to share a single Web 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.

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 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 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.

First the 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>

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);

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.

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

这篇关于共享Web工作人员在单个页面上持续存储重新加载,链接导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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