【问题标题】:Cross Domain Policy troubles non port 80 in Unity跨域策略困扰 Unity 中的非端口 80
【发布时间】:2014-09-22 19:48:33
【问题描述】:

如果 Unity 未在端口 80 上运行,我将无法从 Unity 访问我的 Web 服务。这是我在 Unity 中使用的 C# 代码

private void RunSimulation()
{
    string url = @"http://192.168.1.116:9000/simulate";
    WWW goServer = new WWW(url);
}

在 192.168.1.116 上,我有一个用 go 编写的程序,它正在侦听端口 9000。我可以从 Web 浏览器轻松访问此 URL,我会得到预期的结果。

当我在 Unity 中尝试这个时,如果我检查我的 goServer 变量的内容,我会看到消息

System.Security.Exception:没有可用于允许访问的有效跨域策略

有一个跨域策略文件。它位于 /var/www/html/crossdomain.xml 有以下内容:

<allow-access-from domain="*" to-ports="*" secure="false"/>
<site-control permitted-cross-domain-policies="all"/>

该文件之前存在,并且有.php程序可以成功调用。我添加了 to-ports"*" 试图让它工作,但没有运气。

我尝试让我的 go 服务器监听端口 80。当我这样做时,我可以从 Unity 进行连接。有什么想法吗?


(The input sanitizer is defeating me and I can't figure out how to post without the input form telling me that my links are not allowed, so I'm going to omit 192.168.1.116:9000)

我在 serverurl.com:9000/crossdomain.xml 没有得到任何东西 所以我在我的 go 服务器中放入了一些代码来响应这个请求,并返回一个带有标题 Access-Control-Allow-Origin、Access-Control-Allow-Headers 和 Access-Control-Allow-Credentials 的响应。

我设置了 ENABLE_CROSSDOMAIN_LOGGING 环境变量。我在日志文件中看到了这一点,还有更多

确定请求的 crossdomain.xml 位置:/simulate?asdfa=sadfsa

关于解析url:/simulate?asdfa=sadfsa

确定请求的 crossdomain.xml 位置:/simulate?asdfa=sadfsa

即将解析url:/crossdomain.xml

关于解析url:/simulate?asdfa=sadfsa

Determining crossdomain.xml location for request: /simulate?asdfa=sadfsa

Download had OK statuscode

Received the following crossdomain.xml

----------



----------

received policy

BuildFlashPolicy caught an exception while parsing
/crossdomain.xml: Policy can't be constructed from empty stream.

ArgumentException:无法从空流构造策略。 在 MonoForks.System.Windows.Browser.Net.FlashCrossDomainPolicy.FromStream (System.IO.Stream originalStream) [0x00000] in :0 在 MonoForks.System.Windows.Browser.Net.CrossDomainPolicyManager.BuildFlashPolicy (Boolean statuscodeOK, MonoForks.System.Uri uri, System.IO.Stream responsestream, System.Collections.Generic.Dictionary`2 responseheaders) [0x00000] in :0 UnityEngine.WWW:get_assetBundle() PlayEditorMain:RunSimulation()(在 Assets\Scripts\Play Editor Scripts\Menu Scripts\PlayEditorMain.cs:526) PlayEditorMain:ButtonPressed(GameObject)(在 Assets\Scripts\Play Editor Scripts\Menu Scripts\PlayEditorMain.cs:261) MainMenuButton:OnClick()(在 Assets\Scripts\Play Editor Scripts\Menu Buttons\MainMenuButton.cs:23) UnityEngine.GameObject:SendMessage(String, Object, SendMessageOptions) UICamera:Notify(GameObject, String, Object) (在 Assets\3rd Party\NGUI\Scripts\UI\UICamera.cs:765) UICamera:ProcessTouch(Boolean, Boolean) (在 Assets\3rd Party\NGUI\Scripts\UI\UICamera.cs:1435) UICamera:ProcessMouse()(在 Assets\3rd Party\NGUI\Scripts\UI\UICamera.cs:1063) UICamera:Update()(在 Assets\3rd Party\NGUI\Scripts\UI\UICamera.cs:909)

(文件名:Assets/Scripts/Play Editor Scripts/Menu Scripts/PlayEditorMain.cs 行:526)

无法从 URL (/simulate?asdfa=sadfsa) 确定音频类型。请指定类型。 UnityEngine.WWW:GetAudioClip(Boolean, Boolean, AudioType) UnityEngine.WWW:GetAudioClip(Boolean, Boolean) (在 C:\BuildAgent\work\d63dfc6385190b60\artifacts\EditorGenerated\Utils.cs:308) UnityEngine.WWW:GetAudioClip(Boolean) (在 C:\BuildAgent\work\d63dfc6385190b60\artifacts\EditorGenerated\Utils.cs:301) UnityEngine.WWW:get_audioClip()(在 C:\BuildAgent\work\d63dfc6385190b60\artifacts\EditorGenerated\Utils.cs:293) PlayEditorMain:RunSimulation()(在 Assets\Scripts\Play Editor Scripts\Menu Scripts\PlayEditorMain.cs:526) PlayEditorMain:ButtonPressed(GameObject)(在 Assets\Scripts\Play Editor Scripts\Menu Scripts\PlayEditorMain.cs:261) MainMenuButton:OnClick()(在 Assets\Scripts\Play Editor Scripts\Menu Buttons\MainMenuButton.cs:23) UnityEngine.GameObject:SendMessage(String, Object, SendMessageOptions) UICamera:Notify(GameObject, String, Object) (在 Assets\3rd Party\NGUI\Scripts\UI\UICamera.cs:765) UICamera:ProcessTouch(Boolean, Boolean) (在 Assets\3rd Party\NGUI\Scripts\UI\UICamera.cs:1435) UICamera:ProcessMouse()(在 Assets\3rd Party\NGUI\Scripts\UI\UICamera.cs:1063) UICamera:Update()(在 Assets\3rd Party\NGUI\Scripts\UI\UICamera.cs:909)

[C:/BuildAgent/work/d63dfc6385190b60/Runtime/Audio/AudioClip.cpp 第118行] (文件名:Assets/Scripts/Play Editor Scripts/Menu Scripts/PlayEditorMain.cs 行:526)

所以我认为我现在返回了正确的标题,但我认为我还需要返回正文中的内容。我在想我只会返回与我在端口 80 上查询时得到的 crossdomain.xml 文件相同的内容。

我知道我的服务器上的 crossdomain.xml 端点正在被命中。如果我了解 unity 输出的日志文件是什么,问题可能只是正文为空而我有一个空流。

【问题讨论】:

  • 查看 Unity 请求的实际文件(即使用 Fiddler 捕获请求)。它有可能会在与您预期不同的位置要求文件。
  • 我确实想试试这个,但我还不能让统一使用代理。为 chrome 设置代理并检查 Fiddler 中的流量很容易,但我还不能让它为 Unity 工作。

标签: c# unity3d cross-domain


【解决方案1】:

不需要代理,Unity 内置了跨域日志记录:查看the debuging section of the unity3d manual on the security sandbox 并使用ENABLE_CROSSDOMAIN_LOGGING 打开跨域调试。然后在日志中查找结果。

可以肯定的是,可以通过http://192.168.1.116:9000/crossdomain.xml 访问跨域文件?

【讨论】:

    【解决方案2】:

    我要做的是实现我自己版本的 crossdomain.xml 端点。目前看起来是这样的。

    func crossdomain(res http.ResponseWriter, req *http.Request) { log.Println("跨域顶部")

    if origin := req.Header.Get("Origin"); origin != "" {
        res.Header().Set("Access-Control-Allow-Origin", origin)
    }
    res.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
    res.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token")
    res.Header().Set("Access-Control-Allow-Credentials", "true")
    
    body := "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + 
                "<!DOCTYPE cross-domain-policy SYSTEM \"http://www.adobe.com/xml/dtds/cross-domain-policy.dtd\">" +
                    "<cross-domain-policy>" +
                        "<site-control permitted-cross-domain-policies=\"all\"/>" +
                        "<allow-access-from domain=\"*\" to-ports=\"*\" secure=\"false\"/>" +
                        "<allow-access-from domain=\"*\" to-ports=\"*\"/>" +
                        "<allow-access-from domain=\"*\"/>" +
                        "<allow-http-request-headers-from domain=\"*\"  to-ports=\"*\"/>" +
                        "<allow-http-request-headers-from domain=\"*\"/>" +
                        "<site-control permitted-cross-domain-policies=\"all\"/>" +
                    "</cross-domain-policy>"
    
    io.WriteString(res,body)
    

    }

    func main() {

    http.HandleFunc("/simulate", simulate)
    http.HandleFunc("/crossdomain.xml", crossdomain)
    
    listenErr := http.ListenAndServe(":9000", nil)
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-12
      • 1970-01-01
      • 2011-02-20
      • 2010-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-14
      相关资源
      最近更新 更多