Webplayer socket issue

Hello everybody,

This couple of days I’ve been having some issues trying to set up unity webplayer as a client to a node.js net server. I’m trying to keep the communication to a minimum overhead right now (no http), just some tcp sockets.

I wrote the client side and tested it on the editor without a problem. When I deployed to webplayer, the connection didn’t work anymore.

I searched a LOT around the forum and Unity Answers but couldn’t find an answer.

While trying to solve this I also made a mini node.js server to serve the cross domain policy.

Debugging the webplayer revealed that this call

bool isOk = Security.PrefetchSocketPolicy(server,15354,500);

returns true. My policy looks like this

socket.on('data', function () {
		this.write('<?xml version="1.0"?><cross-domain-policy><allow-access-from domain="*" to-ports="5000"/></cross-domain-policy>');
    });

so i guess the policy is not the issue here.

the next line after prefetch socket policy is
client = new SocketClient();

The first thing I get when i try to step into that code I get this:

System.Security.SecurityManager.ThrowException (ex={System.Security.SecurityManager: Attempt to access a private/protected method failed.
  at System.Security.SecurityManager.ThrowException (System.Exception ex) [0x00000] in <filename unknown>:0 
  at SimpleTcpClient.SocketClient..ctor () [0x00000] in <filename unknown>:0 
  at Network.go () [0x00016] in *******\Assets\Network.cs:54 }) in

And that’s the weird part. Code execution doesn’t even get to call connect. Doesn’t even make it to the constructor. It goes straight to that exception :frowning:

Is the .net Socket class blocked in the webplayer? or maybe another one that i’m using is? here are the members i use in my socket client

		public delegate void VoidDlegate();
		public delegate void StringDlegate(string data);
		public event VoidDlegate OnConnect;
		public event VoidDlegate OnDisconnect;
		public event StringDlegate OnReceive;
		
		public Socket socket;
		Mutex mutex = new Mutex();
		bool _manualDisconnect = false;
		char separator = (char)3;

Any help is appreciated!

I am using Unity 4.3.2f1 free

Since it gave an exception only when trying to instantiate my class, but not reaching the constructor, I started blaming the members. So I commented them one by one, until I found **Mutex mutex = new Mutex();**to be the culprit.

A fast google search lead me to
http://forum.unity3d.com/threads/76087-WebPlayer-Multi-threading
with this solution
Fixed by avoiding mutexes and going with C#'s “lock()”.

It’s funny how I spend so much time thinking this was a networking issue . . .