闪存策略文件不起作用(相同的域,不同的端口)



我不知所措。我无法弄清楚为什么 Flash 无法正确加载我的策略文件。我正在从闪存调试器对此进行测试。

I've also tried from http://127.0.0.1:80/game (it sends a request to http://127.0.0.1:3014/socket.io/1/).
          Security.loadPolicyFile('xmlsocket://127.0.0.1:843');
          var r:URLRequest = new URLRequest();
          r.url = httpProtocal+"://" + domain + "/socket.io/1/?time=" + new Date().getTime();
          r.method = URLRequestMethod.POST;
          var ul:URLLoader = new URLLoader(r);
          ul.addEventListener(Event.COMPLETE, onDiscover);
          ul.addEventListener(HTTPStatusEvent.HTTP_STATUS, onDiscoverError);
          ul.addEventListener(IOErrorEvent.IO_ERROR , onDiscoverError);

错误:

> webSocketLog: policy file: xmlsocket://127.0.0.1:843 Error #2044:
> Unhandled securityError:. text=Error #2048: Security sandbox
> violation: file:///ude/game/bin-release/Game.swf cannot load data from
> http://127.0.0.1:3014/socket.io/1/?time=1359025067289.    at
> com.pnwrain.flashsocket::FlashSocket()

然而,这有效:

> echo -ne '<policy-file-request/>' | nc -v 127.0.0.1 843  
Connection to 127.0.0.1 843 port [tcp/*] succeeded!
<?xml version='1.0' ?>
<!DOCTYPE cross-domain-policy SYSTEM 'http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd'>
<cross-domain-policy>
    <allow-access-from domain='*' to-ports='*' />
</cross-domain-policy>

指定跨域的绝对路径.xml也不起作用。我可以在浏览器中加载此文件。

> webSocketLog: policy file: http://127.0.0.1:843/crossdomain.xml
> Error #2044: Unhandled securityError:. text=Error #2048: Security
> sandbox violation: file:///ude/game/bin-release/Game.swf cannot load
> data from http://127.0.0.1:3014/socket.io/1/?time=1359025126138.  at
> com.pnwrain.flashsocket::FlashSocket()

即使这不起作用(来自一篇非常受欢迎的博客文章的解决方案):

import flash.system.Security; Security.allowDomain("http://127.0.0.1");

下面是调试器策略文件日志:

> OK: Root-level SWF loaded:
> file:///ude/game/bin-release/Game.swf
> OK: Searching for <allow-access-from> in policy files to authorize
> data loading from resource at
> http://127.0.0.1:3014/socket.io/1/?time=1359026453454 by requestor
> from
> file:///ude/game/bin-release/Game.swf
> Error: [strict] Ignoring policy file at
> http://127.0.0.1:3014/crossdomain.xml due to missing Content-Type. 
> See http://www.adobe.com/go/strict_policy_files to fix this problem.
> Error: Request for resource at
> http://127.0.0.1:3014/socket.io/1/?time=1359026453454 by requestor
> from
> file:///ude/game/bin-release/Game.swf
> is denied due to lack of policy file permissions. Warning: HTTP
> response headers not available on this platform.  Strict policy file
> rules cannot be enforced. OK: Policy file accepted:
> http://127.0.0.1:843/crossdomain.xml

这里有趣的部分是,OK: Policy file accepted消息是在错误之后出现的,即使我正在调用:

Security.loadPolicyFile('http://127.0.0.1:843/crossdomain.xml');

URLRequest之前.所以我将URLRequest移动到 setTimeout 中,现在日志显示:

> OK: Root-level SWF loaded:
> file:///ude/game/bin-release/Game.swf
> Warning: HTTP response headers not available on this platform.  Strict
> policy file rules cannot be enforced. OK: Policy file accepted:
> http://127.0.0.1:843/crossdomain.xml OK: Searching for
> <allow-access-from> in policy files to authorize data loading from
> resource at http://127.0.0.1:3014/socket.io/1/?time=1359028255268 by
> requestor from
> file:///ude/game/bin-release/Game.swf
> Error: [strict] Ignoring policy file at
> http://127.0.0.1:3014/crossdomain.xml due to missing Content-Type. 
> See http://www.adobe.com/go/strict_policy_files to fix this problem.

因此,它似乎已正确加载 843 策略文件,但随后尝试加载端口 3014 策略文件(也许检查子策略文件?这只是一个 socket.io HTML页面,上面写着"欢迎来到 socket.io"。似乎不应该因为这个原因而失败。

我在调试器警报中得到这个:

Error #2044: Unhandled securityError:. text=Error #2048: Security sandbox violation: file:///ude/game/bin-release/Game.swf cannot load data from http://127.0.0.1:3014/socket.io/1/?time=1359028255268.
    at MethodInfo-3642()
    at Function/http://adobe.com/AS3/2006/builtin::apply()
    at SetIntervalTimer/onTimer()
    at flash.utils::Timer/_timerDispatch()
    at flash.utils::Timer/tick()

任何想法都非常感谢。谢谢!

我不擅长策略文件,但这看起来很可疑:

错误: [strict] 忽略策略文件 http://127.0.0.1:3014/crossdomain.xml由于缺少内容类型。 请参阅 http://www.adobe.com/go/strict_policy_files 解决此问题。

似乎策略文件被忽略了,因为它没有或无效的内容类型标头。

从 Adobe:

从版本 9,0,115,0 开始,Flash Player 将忽略任何未使用内容类型值发送的 HTTP 策略文件,该值在一定程度上保证该文件旨在成为文本文件。Flash Player 要求策略文件的内容类型必须是以下类型之一:

  • text/* (any text type)
  • application/xmlapplication/xhtml+xml

而这个:

如果您发现需要解决内容类型问题,请务必同时查阅元策略部分,因为选择元策略的常用方法是为所有策略文件指定特殊的text/x-cross-domain-policy内容类型,这可以同时解决两个问题 — 建立元策略和提供文本内容类型。

还要检查您的服务器,为什么 HTTP 响应标头不可用:

警告:HTTP 响应标头在此平台上不可用。 严格 无法强制执行策略文件规则。

希望这有帮助。

成功连接和策略验证的关键是:

1) 在与 socket.io 脚本相同的文件夹中有一个跨域.xml文件。

2) 通过运行 socket.io 服务器的同一主机:端口中的 http 提供此跨域.xml。

3)要实现这一点,除了 socket.io 之外,您还需要安装快速框架。使用 express 和 socket.io,您可以在同一端口上同时通过 http 和连接套接字提供跨域.xml。

4)使用此方法,flash可以在最初查找此策略文件的位置(同一host:port)中找到跨域.xml,无需手动从闪存加载策略文件(您可以禁用loadPolicyFile)

会是这样的:

var app = require('express')();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
app.get('/crossdomain.xml', function (req, res) {
  console.log("request ... " + __dirname);
  res.sendfile(__dirname + '/crossdomain.xml');
});
server.listen(port, "ip");
console.log("socket.io server started");

最新更新