HTML5:使用清单回退来检查用户的连接

发布于 2024-12-22 04:24:57 字数 1092 浏览 6 评论 0原文

我目前专注于 HTML5 的离线 Web 应用程序。我需要一种可靠的方法来检查用户的连接以确定他/她目前是在线还是离线。由于我了解到 navigator.onLine 属性非常不可靠,因此我发现了一个使用缓存清单的后备区域的非常好的方法。我从两本不同的书中实现了两个类似的解决方案,一本是“HTML5 简介”(Lawson/Sharp),一本是“HTML5:缺失的手册”(MacDonald)。我猜这是 HTTP 缓存的问题(我使用 Apache 和 localhost),我对此不太了解。我粘贴了我的代码,它是几个文件,但代码很少。

HTML5简介中的改编解决方案: http://pastebin.com/UGsmnAtK

HTML5 的改编解决方案 - 缺少手册: http://pastebin.com/8v5ck3E6

使用 Chrome 16 进行测试...

=== 我想要的 ===

  • 使用空缓存启动应用程序并运行 apache
  • 单击按钮 ->显示警报“在线”
  • 停止 apache
  • 单击按钮 ->显示警报“离线”
  • 启动 apache
  • 单击按钮 ->显示“在线”警报

=== 发生了什么 ===

介绍 HTML5 解决方案: - 使用空缓存启动应用程序并运行 apache - 点击按钮->显示警报“在线” - 停止阿帕奇 - 点击按钮->显示警报“在线” - 启动阿帕奇 - 点击按钮->显示警报“在线”

HTML5:缺少手动解决方案: - 使用空缓存启动应用程序并运行 apache - 点击按钮->显示警报“在线” - 停止阿帕奇 - 点击按钮->显示警报“在线” - 启动阿帕奇 - 点击按钮->显示警报“在线”

其他场景和用例以类似的方式失败。这些书承诺您可以使用他们的方法随时检查用户的连接情况。所以我想我在这里做错了什么。我很高兴接受有关这个主题的任何想法。

干杯, 菲利克斯

I'm focusing on offline web applications with HTML5 at the moment. I came to the point where I need a reliable method of checking the user's connection to determine whether he/she is online or offline at the moment. Since I learned that the navigator.onLine property is highly unreliable I found a very nice method using the Fallback area of the cache manifest. I implemented two similar solutions from two different books, one being "Introducing HTML5" (Lawson/Sharp) and one "HTML5: The Missing Manual" (MacDonald). I guess this is an issue of HTTP caching (I use Apache and localhost), which I don't really know too much about. I pasted my code, it's a few files, but very little code.

The adapted solution from Introducing HTML5:
http://pastebin.com/UGsmnAtK

The adapted solution from HTML5 - the missing manual:
http://pastebin.com/8v5ck3E6

Tested with Chrome 16...

=== What I want ===

  • start app with empty cache and running apache
  • click the button -> alert "Online" is shown
  • stop apache
  • click the button -> alert "Offline" is shown
  • start apache
  • click the button -> alert "Online" is shown

=== What happens ===

Introducing HTML5 solution:
- start app with empty cache and running apache
- click the button -> alert "Online" is shown
- stop apache
- click the button -> alert "Online" is shown
- start apache
- click the button -> alert "Online" is shown

HTML5: the missing manual solution:
- start app with empty cache and running apache
- click the button -> alert "Online" is shown
- stop apache
- click the button -> alert "Online" is shown
- start apache
- click the button -> alert "Online" is shown

Other scenarios and use cases fail in similar fashion. The books promise that you can check the user's connectivity any time using their methods. So I guess I'm doing something wrong here. I would thankfully embrace any ideas on this topic.

Cheers,
Felix

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

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

发布评论

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

评论(1

郁金香雨 2024-12-29 04:24:57

问题在于,当您单击“介绍 HTML5”按钮或使用 HTML5 加载页面时,就会加载 online.js - 缺少手动代码,并且该站点可通过 Apache 访问。下次不会加载脚本,因为它已经在浏览器缓存中。

Introducing HTML5 中的 coce 看起来如果您向脚本 URL 添加随机值,它应该可以工作。像这样的东西:

function testOnline(fn) {
    var script = document.createElement('script');
    script.src = 'online.js?r=' + Math.random();

    window.setOnline = function(online) {
        document.body.removeChild(script);
        fn(online);
    }

    document.body.appendChild(script);
}

The problem is that the online.js is loaded when you click the button with the Introducing HTML5 or when the page is loaded with the HTML5 - the missing manual code and the site is available through Apache. Next time the scripts isn't loaded as it is already in the browser cache.

The coce from Introducing HTML5 looks like it should work if you add a random value to the script URL. Something like:

function testOnline(fn) {
    var script = document.createElement('script');
    script.src = 'online.js?r=' + Math.random();

    window.setOnline = function(online) {
        document.body.removeChild(script);
        fn(online);
    }

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