zoukankan      html  css  js  c++  java
  • Unity3d Web Player 与server端联网配置

    针对Unity3d Web Player 的server端联网配置写一随笔咯。
      以SmartFoxServer2X官方的Unity3d Example ”tris“为例,部署好服务器之后,在Unity端跑游戏肯定没问题,成功连接。但是当切换到Web Player打包方式,并且确定你连接的不是本机服务器,即--服务器端地址不为“LocalHost”或“127.0.0.1”时,死活连接不上服务器了,不论是直接在Unity3D Editor中跑游戏还是打包好Web Player程序在浏览器中跑。其中主要的错误描述为:

    [SFS DEBUG] TCPSocketLayer: General exception on connection: Unable to connect, as no valid crossdomain policy was found   at System.Net.Sockets.Socket.Connect_internal (IntPtr sock, System.Net.SocketAddress sa, System.Int32& error, Boolean requireSocketPolicyFile) [0x00000] in <filename unknown>:0 
      at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP, Boolean requireSocketPolicy) [0x00000] in <filename unknown>:0 
      at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x00000] in <filename unknown>:0 
      at System.Net.Sockets.Socket.Connect (System.Net.IPAddress address, Int32 port) [0x00000] in <filename unknown>:0 
      at Sfs2X.Core.Sockets.TCPSocketLayer.ConnectThread () [0x00000] in <filename unknown>:0
    
    

    这都是Unity WebPlayer Security SandBox机制的问题,做过Flash开发的朋友应该都知道。Unity3D官方的文档中解释这种现象:This security restrictions apply only to the webplayer, and to the editor when the active build target is WebPlayer. 和我遇到的现象描述相符。说白了就是Unity3d为Web Player平台搞了一个security SandBox机制,Only在Web Player的安全机制中,我们在使用Socket时需要服务器配置一个服务安全策略。因为没有在这方面进行任何处理,所以Security SandBox阻止了程序的Socket连接,造成了以上的现象。OK,问题找到了。
      解决办法是:Unity提供了一个“sockpol.exe”这么一个工具,在“...UnityEditorDataTools SocketPolicyServer“路径下有sockpol.exe和它的源码。如果你的服务器端是Windows平台的话,直接Copy一个 sockpol.exe到服务器端,在CMD中执行
      

    sockpol.exe --all

    即可为服务器端配置好Security SandBox安全策略。
      说到这了,如果不认真读Unity3D官方关于Security SandBox的文档是不是还是有点云里雾里的,不禁要问了:这个sockpol.exe是什么神奇的东西呢?
      OK,我们可以不读官方文档,来看一看sockpol.exe的源代码吧,刚才说了在“...UnityEditorDataTools SocketPolicyServer“路径下有sockpol.exe的源码,从源码中很容易就分析出原来sockpol.exe干的活就是监听 Web Player平台获取Security SandBox安全策略时需要连接服务器端的843端口,监听到843端口有请求时,发送给请求的客户端一个 crossdomain.xml配置,内容为标准的crossdomain.xml文件格式:

    <?xml version="1.0"?>
    <cross-domain-policy>
    <allow-access-from domain="*" to-ports="1-65536"/>
    </cross-domain-policy>

    这样客户端就能获取到Security SandBox安全策略并进行网络活动了。其中,执行sockpol.exe的参数--all的意义就是设置服务器的Security SandBox安全策略为允许任何IP访问服务器的任何端口。
      知道了这个原理,Linux服务器端就很容易能得出解决方案了,我们利用Linux的NetCat(NC)工具写一个脚本,以达到同样的目的。
      首先,确认Linux服务器安装了NetCat,在SHELL中键入’NetCat‘或者‘NC’测试一下你的系统中有没有安装这个工具。如果没有反应,很简单,安装一个。
      

    #如果你用RedLinux或者RL系的Linux:
    sudo yum install nc
    #如果你用UbuntuDebian之类Linux:
    sudo apt-get install nc

    安装NC后,写一个脚本:

    #!/bin/sh
    while true; do echo '<?xml version="1.0"?>
    <cross-domain-policy>
    <allow-access-from domain="*" to-ports="1-65536"/>
    </cross-domain-policy>' | nc -l 843; done

    保存为serverPolicy.sh
    别忘了拿到脚本权限

    sudo chmod 755 serverPolicy.sh


    直接运行脚本:

    [csharp] view plaincopyprint?
    1. sudo ./serverPolicy.sh  

    如果没有报错的话,OK,成功为服务器端设置了Unity3D Web Player平台的Security SandBox安全策略。接下来做的就是测试咯!
    对了,直接跑这个脚本的话会很麻烦,因为这时脚本还依赖于SHELL,当我们断开SHELL或者要在SHELL做其他活动时,脚本会停止运行。让脚本在后台运行的方法:

    sudo nohup ./serverPolicy.sh &
  • 相关阅读:
    PHP调用WCF提供的方法
    关于git报 warning: LF will be replaced by CRLF in README.md.的警告的解决办法
    vue中引入mui报Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them的错误
    微信小程序报Cannot read property 'setData' of undefined的错误
    Vue那些事儿之用visual stuido code编写vue报的错误Elements in iteration expect to have 'v-bind:key' directives.
    关于xampp中无法启动mysql,Attempting to start MySQL service...的解决办法!!
    PHP的环境搭建
    新手PHP连接MySQL数据库出问题(Warning: mysqli_connect(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: YES))
    手机号码、获得当前时间,下拉框,填写限制
    团队作业(五):冲刺总结
  • 原文地址:https://www.cnblogs.com/2Yous/p/3357961.html
Copyright © 2011-2022 走看看