Smartfox web player will not connect (no problems with standalone)

My standalone version of a SmartFox project can connect without problems with a local SmartFoxServer or one on the network. However, if I compile the web player or switch platform to web player in the build settings, I get nothing.

It seems the SmartFoxClient get's initialized without problems (at least no errors with debug set to true), but the connection is never made. In OnGUI() I check for smartFox.IsConnected() which never returns true. Also, OnConnection is never called. Below the relevant code. I'm trying to setup the connection in Awake(). I copied the code from the Island demo.

void Awake() {
    Application.runInBackground = true;

    if ( SmartFox.IsInitialized() ) {
        Debug.Log("SmartFox is already initialized, reusing connection");
        smartFox = SmartFox.Connection;
    } else {
        try {
            Debug.Log("Starting new SmartFoxClient");
            smartFox = new SmartFoxClient(debug);
            //smartFox.runInQueueMode = true;
        } catch ( Exception e ) {
            loginErrorMessage = e.ToString();
            Debug.Log(loginErrorMessage);
        }
    }
    Debug.Log("Attempting to connect to SmartFoxServer");
    smartFox.Connect(serverIP, serverPort);

    // Register callback delegate
    SFSEvent.onConnection += OnConnection;
    SFSEvent.onConnectionLost += OnConnectionLost;
    SFSEvent.onLogin += OnLogin;
    SFSEvent.onRoomListUpdate += OnRoomList;
    SFSEvent.onDebugMessage += OnDebugMessage;
}

What am I doing wrong here?

Turns out Unity 3 has incorporated more security in the webplayer. In order to make it work, you need to prefetch the policy file with Security.PrefetchSocketPolicy(serverIP, serverPort); However, this breaks compiling for PC Standalone! I therefore added an if statement checking for the platform that the game is running on. I found this solution in the SmartFox forums (specifically this post). This is the now working code:

    void Awake() {
    Application.runInBackground = true;

    if ( SmartFox.IsInitialized() ) {
        Debug.Log("SmartFox is already initialized, reusing connection");
        smartFox = SmartFox.Connection;
    } else {
        if( Application.platform == RuntimePlatform.WindowsWebPlayer ) {
            Security.PrefetchSocketPolicy(serverIP, serverPort);
        }
        try {
            Debug.Log("Starting new SmartFoxClient");
            smartFox = new SmartFoxClient(debug);
            smartFox.runInQueueMode = true;
        } catch ( Exception e ) {
            loginErrorMessage = e.ToString();
            Debug.Log(loginErrorMessage);
        }
    }

    // Register callback delegate, before callling Connect()
    SFSEvent.onConnection += OnConnection;
    SFSEvent.onConnectionLost += OnConnectionLost;
    SFSEvent.onLogin += OnLogin;
    SFSEvent.onRoomListUpdate += OnRoomList;
    SFSEvent.onDebugMessage += OnDebugMessage;

    Debug.Log("Attempting to connect to SmartFoxServer");
    smartFox.Connect(serverIP, serverPort);     
}

Some related info on the what and why of the improved security of the webplayer in Unity 3 can be found in the user manual here.

You should register your delegates before calling Connect(), especially the onDebugMessage one.

Also, I'm curious why you are not running in queue mode.

Hello

I can't make island demo work either on a webplayer. The sfs pro server won't work. I've replaced the code that you proposed but I get an error on the Debug.Log(loginErrorMessage); line.

should I register the delegates and how can I do that? Any help?