Unity3d Web Player 的server端联网配置

新游戏出了第一个能跑完流程的版本,不得不佩服Unity3D强大的功力,PC、MAC OS、Linux、IOS、Android、web player,前天刚发布的unity3d 4.2版本还支持WINDOWS PHONE、Black Barry这些平台统统一键打包,so easy!不过在打包部署Web Player时,在联网方面出现了一点麻烦,算是顺风顺水过程当中的一个小插曲吧!所以又可以针对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机制的问题。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”这么一个工具,在“...\Unity\Editor\Data\Tools\SocketPolicyServer“路径下有sockpol.exe和它的源码。如果你的服务器端是Windows平台的话,直接Copy一个sockpol.exe到服务器端,在CMD中执行

  

sockpol.exe --all

即可为服务器端配置好Security SandBox安全策略。

  说到这了,如果不认真读Unity3D官方关于Security SandBox的文档是不是还是有点云里雾里的,不禁要问了:这个sockpol.exe是什么神奇的东西呢?

  OK,我们可以不读官方文档,来看一看sockpol.exe的源代码吧,刚才说了在“...\Unity\Editor\Data\Tools\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
#如果你用Ubuntu\Debian之类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

 

直接运行脚本:

  1. sudo ./serverPolicy.sh  
sudo ./serverPolicy.sh

如果没有报错的话,OK,成功为服务器端设置了Unity3D Web Player平台的Security SandBox安全策略。接下来做的就是测试咯!

对了,直接跑这个脚本的话会很麻烦,因为这时脚本还依赖于SHELL,当我们断开SHELL或者要在SHELL做其他活动时,脚本会停止运行。让脚本在后台运行的方法:

sudo nohup ./serverPolicy.sh &

OK,enjoy!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
unity web playerWeb Player License Agreement PLEASE READ CAREFULLY: BY INSTALLING THE SOFTWARE (AS DEFINED BELOW), YOU (EITHER ON BEHALF OF YOURSELF AS AN INDIVIDUAL OR ON BEHALF OF AN ENTITY AS ITS AUTHORIZED REPRESENTATIVE) AGREE TO ALL OF THE TERMS OF THIS END USER LICENSE AGREEMENT REGARDING THE USE OF THE SOFTWARE. 1) GRANT OF LICENSE: You may install this Software on your computer to experience Unity web content. 2) TITLE: You acknowledge that no title to the intellectual property in the Software is transferred to you. Title, ownership, rights, and intellectual property rights in and to the Software shall remain that of Unity Technologies. The Software is protected by copyright laws of the United States and international treaties. 3) ANONYMOUS USAGE STATISTICS: You accept that the first time the Unity Web Player is used, anonymous information about the computer it's loaded on is submitted to Unity Technologies ApS. This only happens once time, and contains no personally identifiable information. The information submitted is: (a) Operating system and version (b) The make of the CPU, and number of CPUs present (c) The graphics card type and vendor name (d) Graphics card driver name and version (example: "nv4disp.dll 6.10.93.71") (e) Which graphics API is in use (example: "OpenGL 2.1" or "Direct3D 9.0c") (f) Amount of system and video RAM present (g) Current desktop resolution (h) Version of the Unity Web Player (i) A number describing whether running on Mac or Windows (j) A checksum of all the data that gets sent to verify that it did transmit correctly 3) DISTRIBUTION: You acknowledge that only Unity Technologies ApS and its designated distribution partners may distribute the Unity Web Player, without a special permission.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值