我监控(并记录到服务器)我们 Flash 游戏中的大多数用户错误。我经常看到与尝试向跨域 URL(通常是 Facebook Graph API)发出请求相关的安全错误。我们 99% 的玩家都可以毫无问题地进行这些图形 API 调用。
我认为是客户端发出请求,但无法加载 crossdomain.xml
文件。我不太清楚 AS3 在加载跨域策略文件失败的情况下如何处理这个问题......它会重试每个 URLRequest
直到成功加载它,还是只是永远放弃?应对此类安全错误的“最佳做法”是什么?
我提前预加载了一次 Facebook 策略文件,如下所示:
// allow images to be loaded from facebook and facebook's cdn's.
Security.loadPolicyFile( "http://www.facebook.com/crossdomain.xml" );
Security.loadPolicyFile( "https://api.facebook.com/crossdomain.xml" );
Security.loadPolicyFile( "https://graph.facebook.com/crossdomain.xml" );
Security.loadPolicyFile( "http://profile.ak.fbcdn.net/crossdomain.xml" );
然后在发出 URLRequest
时,我还会再次快速检查策略文件。
最佳答案
这是一个似乎有效的解决方案。
假设您正在使用 URLLoader
从具有 crossdomain.xml 文件的域之一读取数据,并且您正在调用 loadPolicyFile
来预加载 crossdomain.xml,那么负载有可能因网络连接问题或服务器停机或太阳耀斑而失败。当您设置您的 URLLoader
时,您可以为 SecurityErrorEvent.SECURITY_ERROR
添加一个事件监听器。在 even 监听器中,您可以尝试再次加载策略文件。尽管策略文件会被缓存,即使它们无法加载(感谢 adobe),因此您必须添加一个缓存清除查询参数。
这是一个简单的例子,说明它是如何工作的:
public function loadMyFriends():void {
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(
SecurityErrorEvent.SECURITY_ERROR, handleSecurityError);
urlLoader.load(new URLRequest('https://graph.facebook.com/me/friends'));
}
private function handleSecurityError(event:Event):void {
Security.loadPolicyFile(
"https://graph.facebook.com/crossdomain.xml?__cb"=Math.random());
loadMyFriends();
}
在实践中,如果确实是网络连接问题而不仅仅是负载均衡器未正确处理的死机服务器,您可能希望限制重试次数并可能执行指数回退。
关于flash - 加载crossdomain.xml策略文件时出现网络故障怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7398759/