http://blog.csdn.net/dyllove98/article/details/9501695
The new game out of the first version of the process, have to admire Unity3d powerful power, PC, MAC OS, Linux, IOS, Android, web Player, the day before yesterday released the Unity3d 4.2 version also supports Windows PHONE, Black Barry, these platforms are all packaged in one click, so easy! However, when you packaged the Web player deployment, there was a bit of a hassle in networking, and it was a small episode in the Wind. So you can write an essay on the server-side networking configuration for Unity3d Web Player.
Take SMARTFOXSERVER2X official Unity3d Example "Tris" as an example, after the deployment of the server, the unity side running the game is sure to be no problem, successfully connected. But when you switch to Web Player packaging and make sure you're not connected to a native server--the server-side address is not "LocalHost" or "127.0.0.1," you'll find a "fun" phenomenon (it's not fun at all, but it's been a long time-_-!~ )--dead or alive connected to the server, whether directly in the Unity3d editor run the game or packaged the Web player program in the browser run. The main error descriptions are:
[SFS DEBUG] Tcpsocketlayer:general exception on connection:unable to connect, as no valid Crossdomain policy was fo und at System.Net.Sockets.Socket.Connect_internal (IntPtr sock, System.Net.SocketAddress SA, system.int32& error, B Oolean 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.Socket S.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
After groping through the data found that this is unity Webplayer security sandbox mechanism problem. This phenomenon is explained in the official Unity3d documentation: This security restrictions apply only to the Webplayer, and the editor when the active build target Is Webplayer. Matches the description of the phenomenon I encountered. To be blunt, Unity3d has a security sandbox for the Web player platform, and only in the Web player safety mechanism, we need the server to configure a service security policy when using the socket. Because there is no processing in this area, the security sandbox prevents the socket connection of the program, causing the above phenomenon. OK, the problem is found.
The solution is that Unity provides a "sockpol.exe" tool that has sockpol.exe and its source code under the "... \unity\editor\data\tools\socketpolicyserver" path. If your server is a Windows platform, copy a Sockpol.exe directly to the server side and execute it in CMD.
Sockpol.exe--all
Secure sandbox security policy is configured for the server side.
Speaking of this, if not seriously read Unity3d official about security sandbox document is still a little foggy, can't help asking: This sockpol.exe is what magical things?
OK, we can not read the official documents, take a look at the source of Sockpol.exe, just said in the "... \unity\editor\data\tools\socketpolicyserver" The path has Sockpol.exe source code, from the source code is easy to analyze the original Sockpol.exe the job is to listen to the Web player platform to obtain security sandbox safety policy needs to connect to the server side of the 843 port, listening to 843 port on request , a crossdomain.xml configuration is sent to the requesting client, and the content is in the standard crossdomain.xml file format:
<?xml version= "1.0"? ><cross-domain-policy><allow-access-from domain= "*" to-ports= "1-65536"/> </cross-domain-policy>
This will enable the client to obtain security policy and network activity. Among them, the significance of executing Sockpol.exe parameter--all is to set the server security sandbox to any port that allows any IP access to the server.
Knowing this principle, the Linux server can easily get a solution, we use the Linux Netcat (NC) tool to write a script to achieve the same purpose.
First, verify that the Linux server has NetCat installed, type ' NetCat ' or ' NC ' in the shell to test if the tool is installed on your system. If there is no response, it is simple to install one.
#如果你用RedLinux或者RL系的Linux: sudo yum install nc# If you use Ubuntu\debian like Linux:sudo apt-get install NC
After installing NC, write a script:
#!/bin/shwhile 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
Save As Serverpolicy.sh
Don't forget to get the script permissions
sudo chmod 755 serverpolicy.sh
Run the script directly:
[CSharp]View Plaincopyprint?
- sudo./serverpolicy.sh
If there is no error, OK, successfully set the Unity3d Web player platform Security sandbox safety policy for the server side. The next thing to do is test it!
By the way, it would be troublesome to run this script directly, because the script also relies on the shell, and when we break the shell or do other activities in the shell, the script stops running. Ways to let scripts run in the background:
sudo nohup./serverpolicy.sh &
ok,enjoy!
Unity3d server-side networking configuration for Web Player