【问题标题】:Server sent events - cross domain safari服务器发送事件 - 跨域 safari
【发布时间】:2013-11-03 17:58:21
【问题描述】:

我正在开发一个应用程序来检查我的数据库是否已更新,如果是,我的应用程序会显示新值。

客户端:

if(typeof(EventSource)!=="undefined") {
  var eSource = new EventSource("http://mydomain.org/app.php");
  eSource.onmessage = function(event) {}
}

服务器端:

<?php
header("Content-Type: text/event-stream");
header("Cache-Control: no-cache");
header("Access-Control-Allow-Origin: *");
echo "data:Hello World\n\n";
?>

现在,一切都在 Firefox 和 Chrome 中运行。该应用程序将在基于 Safari 的浏览器中运行。在 Safari 中,我得到 ​​p>

SECURITY_ERR: DOM Exception 18: An attempt was made to break through the security policy of the user agent.

所以这个问题与跨域发布有关,即使 Access-Control-Allow-Origin 有一个通配符。有什么方法可以在 Safari 中完成这项工作?

【问题讨论】:

    标签: php safari cross-domain server-sent-events


    【解决方案1】:

    WebKit 尚不支持 SSE 中的 CORS。解决方案:

    1. 同源获取,new EventSource("/app.php");

    2. 为 Safari 使用可识别 CORS 的 polyfill。 https://github.com/Yaffle/EventSource

    3. 在与您的 SSE 流 (http://mydomain.org/iframe.html) 相同的来源创建 iframe,在 iframe 中读取同源流,并使用postMessage() 将事件发送到其他来源。

    【讨论】:

    • 解决方案 1:HTML 文件必须从硬盘驱动器运行(如浏览器中的小部件)。解决方案 3:用户可以为样式设置参数,因此使用 iFrame,无法为来自其他来源的元素设置样式。看来我将不得不研究解决方案 2。
    • @TomBroucke iframe 可能是不可见的。
    • 是的,我以前没有使用过 postMessage() ,但这似乎是解决我的问题的一个很好的方法。谢谢
    【解决方案2】:

    什么版本的 Safari? 我相信 EventSource 的 CORS 在 Chrome 25+ 和 Safari 6.1+ 之前都被破坏了。

    【讨论】:

      猜你喜欢
      • 2018-02-27
      • 2012-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-24
      • 2015-06-16
      • 2013-08-20
      • 1970-01-01
      相关资源
      最近更新 更多