I have 2 projects on the same PC which both use Security.PrefetchSocketPolicy(serverName, serverPort, 500) as the first thing in their Start() function. One works, the other one does not. I don´t understand how this can be.
I created a new scene in my project (the one that does not work),
put an empty GameObject in its Hierarchy,
copied the C# script from the working project into the not working one,
and then attached the script to the GameObject.
This scene has no connection to anything else in the project. When I start it in the editor: it does not work. Why?
The project name is diffenent and the location on the harddrive, (obviously)
There are more files in the project folder (and therefore project view) in the not working project (but non of them is being used in the test scene!).
Other than that I don´t see any difference between the two projects but one works and the other one does not:
That is the C# script for both:
using UnityEngine;
using System;
using System.Collections.Generic;
public class Co : MonoBehaviour {
#region setup variables
public static string serverName = "127.0.0.1";
public static int serverPort = 9933;
#endregion
void Start () {
// In a webplayer (or editor in webplayer mode) we need to setup security policy negotiation with the server first
if (Application.isWebPlayer || Application.isEditor) {
if (!Security.PrefetchSocketPolicy(serverName, serverPort, 500)) {
Debug.LogError("Security Exception. Policy file load failed!");
}
else {
Debug.Log ("it works!!!");
}
}
}
}
Of course it won’t work if the server can’t be reached on that IP and port, or if the server doesn’t serve a socket policy in the right form. It also won’t work if you use a hostname instead of an IPv4 dotted IP address, so if there’s a possibility of a hostname (as when users enter the IP manually) I process the input first:
Is it possible that your server might not be responding quickly enough? Perhaps you need a longer timeout. I don’t bother to specify, so I get the default which is 3000.
Thank you for your response. But this does not work either.
The Problem is, that the same code on the same PC with the same server works in one project but not in another (all I do is open another project in Unity and run it).
I tried timeout values up to 3000 with no success. I also tried your code, but in editor mode this is not even executed. It tried this then:
#if UNITY_EDITOR
if (!Security.PrefetchSocketPolicy(serverName, serverPort, 3000)) {
Debug.LogError("Security Exception. Policy file load failed!");
}
Debug.Log ("I get here");
#endif
but it gives me the error message.
I suspect that something in the unity project got messed up. I can’t see any difference between the two projects other than there are more files in one of them.
Can someone think of anything that I could look into? Is there any way to get some information out of this Policy thing on why it exactly failed?
Try changing your IF to “#if UNITY_WEBPLAYER”. The Prefetch line should only be used for webplayer - it will give you an error with any other kind of player.
If I do this I can´t be certain if it works. The policy test won´t be executed. I would need to build a webplayer and there I don´t see my debug log to check whether it worked or not.
Since it works in one project I don´t see why it shouldn´t work in another. Even in Editor mode.
I need to be absolutely certain that it works. Right now this is completely random behavior that I do not understand. That is something I just can´t live with.
The policy test will break your build if it’s not a web player build. In my experience, if I have the build set to web player and I’m running from the editor, I need to have the policy test in there, and if I have the build set to some other type of player and I’m running from the editor, I need to have it NOT there. That particular IF statement works for me no matter what build I have.
Hopefully eventually Unity will fix it so that the prefetch line doesn’t break the non-webplayer builds. We can hope at least.
Oh, here you said something. When ever I was building a test build for web I just did so and never had a problem until I started to use client-server stuff. I never noticed the “switch platform” button underneath the list of platforms. I had no idea that the editor could be put into a platform mode itself.
Now with what you where saying here I started to look around for such setting and found this button. And that actually works! Now I also understand what you really meant with that IF statement.