跨域策略在Unity中非端口80出现问题

时间:2014-07-31 00:40:11

标签: c# unity3d cross-domain

如果没有在端口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上,我有一个正在使用端口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“*”以试图让它工作,但没有运气。

我试过让我的服务器在端口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上没有得到任何东西 因此,我将一些代码放入我的服务器以响应此请求,并返回带有标头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:通知(GameObject,String,Object)(在Assets \ 3rd Party \ NGUI \ Scripts \ UI \ UICamera.cs:765) UICamera:ProcessTouch(布尔值,布尔值)(在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 Line:526)

无法从URL确定音频类型(/ simulate?asdfa = sadfsa)。请指定类型。 UnityEngine.WWW:GetAudioClip(Boolean,Boolean,AudioType) UnityEngine.WWW:GetAudioClip(布尔值,布尔值)(在C:\ BuildAgent \ work \ d63dfc6385190b60 \ artifacts \ EditorGenerated \ Utils.cs:308) UnityEngine.WWW:GetAudioClip(布尔)(在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:通知(GameObject,String,Object)(在Assets \ 3rd Party \ NGUI \ Scripts \ UI \ UICamera.cs:765) UICamera:ProcessTouch(布尔值,布尔值)(在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 Line:526)

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

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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

我要做的是实现我自己的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)

}

相关问题