SmartFox SFSEvent.CONNECTION isn't calling

Hello! I downloaded the SmartFoxServer2X examples a few weeks ago and started messing with the FPS game.

Now, when I insert the FPS game package to a new project and test it with my server, it works fine; however, when I wrote something similar to the FPS game LoginGUI code on my project, the ‘OnConnection’ or any of the EventListeners I added will not call at all.

Here is the code:

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using Sfs2X;
using Sfs2X.Core;
using Sfs2X.Entities;
using Sfs2X.Requests;
using Sfs2X.Logging;

public class LoginGUI : MonoBehaviour 
{
	#region SmartFox
	private SmartFox smartFox;
	#endregion
	
	#region Variables
	private bool shuttingDown = false;
	public string serverName = "XXX.XXX.XXX.XXX";
	private string username = "";
	private string loginErrorMessage = "";
	public int serverPort = 9933;
	public string zone = "Nomsoft";
	public bool debug = true;
	#endregion
	
	#region Skin
	public GUISkin loginGUISkin;
	#endregion
	
	void OnApplicationQuit()
	{
		shuttingDown = true;
	}
	
	void FixedUpdate()
	{
		smartFox.ProcessEvents();
	}
	
	void Awake()
	{
		Application.runInBackground = true;
		
		if (Application.isWebPlayer || Application.isEditor) {
			if (!Security.PrefetchSocketPolicy(serverName, serverPort, 500)) {
			}
		}
		
		if (SmartFoxConnection.IsInitialized)
			smartFox = SmartFoxConnection.Connection;
		else
			smartFox = new SmartFox();
		
		smartFox.AddEventListener(SFSEvent.CONNECTION, OnConnection);
		smartFox.AddEventListener(SFSEvent.CONNECTION_LOST, OnConnectionLost);
		smartFox.AddEventListener(SFSEvent.LOGIN, OnLogin);
		smartFox.AddEventListener (SFSEvent.UDP_INIT, OnUdpInit);
		
		smartFox.AddLogListener (LogLevel.DEBUG, OnDebugMessage);
		smartFox.Connect (serverName, serverPort);
	}
	
	#region GUI
	void OnGUI()
	{
		GUI.skin = loginGUISkin;
		
		if (smartFox.IsConnected)
		{
			DrawLoginGUI();
		}
		else
		{
			string message = "Waiting for connection";
			if (loginErrorMessage != "")
			{
				message = "Connection error : " + loginErrorMessage;
			}
			DrawMessagePanelGUI(message);
		}
	}
	
	// Generic single message panel
	void DrawMessagePanelGUI(string message) {
		// Lets just quickly set up some GUI layout variables
		float panelWidth = 400;
		float panelHeight = 300;
		float panelPosX = Screen.width/2 - panelWidth/2;
		float panelPosY = Screen.height/2 - panelHeight/2;
		
		// Draw the box
		GUILayout.BeginArea(new Rect(panelPosX, panelPosY, panelWidth, panelHeight));
		GUILayout.Box ("FPS Example", GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
		GUILayout.BeginVertical();
		GUILayout.BeginArea(new Rect(20, 25, panelWidth-40, panelHeight-60), GUI.skin.customStyles[0]);
		
		// Center label
		GUILayout.BeginHorizontal();
		GUILayout.FlexibleSpace();
		GUILayout.BeginVertical();
		GUILayout.FlexibleSpace();
			
		GUILayout.Label(message);
			
		GUILayout.FlexibleSpace();
		GUILayout.EndVertical();
		GUILayout.FlexibleSpace();
		GUILayout.EndHorizontal();
		
		GUILayout.EndArea ();		
			
		GUILayout.EndVertical();
		GUILayout.EndArea ();		
	}
	
	// Login GUI allowing for username, password and zone selection
	private void DrawLoginGUI() {
		// Lets just quickly set up some GUI layout variables
		float panelWidth = 400;
		float panelHeight = 300;
		float panelPosX = Screen.width/2 - panelWidth/2;
		float panelPosY = Screen.height/2 - panelHeight/2;
		
		// Draw the box
		GUILayout.BeginArea(new Rect(panelPosX, panelPosY, panelWidth, panelHeight));
		GUILayout.Box ("Login", GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
		GUILayout.BeginVertical();
		GUILayout.BeginArea(new Rect(20, 25, panelWidth-40, panelHeight-60), GUI.skin.customStyles[0]);
		
		// Lets show login box!
		GUILayout.FlexibleSpace();
				
		GUILayout.BeginHorizontal();
		GUILayout.Label("Username: ");
		username = GUILayout.TextField(username, 25, GUILayout.MinWidth(200));
		GUILayout.EndHorizontal();

		GUILayout.BeginHorizontal();
		GUILayout.Label("Server Name: ");
		serverName = GUILayout.TextField(serverName, 25, GUILayout.MinWidth(200));
		GUILayout.EndHorizontal();

		GUILayout.BeginHorizontal();
		GUILayout.Label("Server Port: ");
		serverPort = int.Parse(GUILayout.TextField(serverPort.ToString(), 4, GUILayout.MinWidth(200)));
		GUILayout.EndHorizontal();
		/*
		GUILayout.BeginHorizontal();
		GUILayout.Label("Invert Mouse Y: ");
		OptionsManager.InvertMouseY = GUILayout.Toggle(OptionsManager.InvertMouseY, "");
		GUILayout.EndHorizontal();*/
		
		GUILayout.Label(loginErrorMessage);
			
		// Center login button
		GUILayout.BeginHorizontal();
		GUILayout.FlexibleSpace();		
		if (GUILayout.Button("Login")  || (Event.current.type == EventType.keyDown && Event.current.character == '

')) {
Debug.Log(“Sending login request”);
smartFox.Send(new LoginRequest(username, “”, zone));
}
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
GUILayout.FlexibleSpace();

		GUILayout.EndArea ();		
				
		GUILayout.EndVertical();
		GUILayout.EndArea ();		
	}
	#endregion
	
	private void UnregisterSFSSCallBacks()
	{
		smartFox.RemoveAllEventListeners();
	}
	
	void OnConnection(BaseEvent m_event)
	{
		bool success = (bool)m_event.Params["success"];
		string error = (string)m_event.Params["error"];
		
		Debug.Log("Connection : " + success);
		Debug.Log("Connection Error : <" + error + ">");
		
		loginErrorMessage = "";
		if (success)
			SmartFoxConnection.Connection = smartFox;
		else
			loginErrorMessage = error;
	}
	
	void OnLogin(BaseEvent m_event)
	{
		bool success = true;
		if (m_event.Params.ContainsKey("success") && !(bool)m_event.Params["success"])
		{
			loginErrorMessage = (string)m_event.Params["errorMessage"];
			Debug.Log("Error Logging In : " + loginErrorMessage);
		}
		else
		{
			Debug.Log ("Logged in Successfully");
			smartFox.InitUDP (serverName, serverPort);
		}
	}
	
	void OnUdpInit(BaseEvent m_event)
	{
		if (m_event.Params.ContainsKey("success") && !(bool)m_event.Params["success"])
		{
			loginErrorMessage = (string)m_event.Params["errorMessage"];
			Debug.Log ("UDP Error : " + loginErrorMessage);
		}
		else
		{
			Debug.Log ("UDP is okay!");
			loginErrorMessage = "";
			UnregisterSFSSCallBacks ();
			Application.LoadLevel ("Scene");
		}
	}
	
	void OnDebugMessage(BaseEvent m_event)
	{
		string msg = (string)m_event.Params["message"];
		Debug.Log ("[DEBUG]: " + msg);
	}
	
	void OnConnectionLost(BaseEvent m_event)
	{
		loginErrorMessage = "Connection Lost";
		UnregisterSFSSCallBacks ();
	}
}

As I said, it is very similar to the FPS game example code. I will repeat the issue in a different term:

When starting my client up, the session will be created, followed by these messages (ServerSide):

12 Dec 2012 | 22:28:46,242 | INFO | SocketReader | bitswarm.core.SocketAcceptor | Session created: { Id: 23, Type: DEFAULT, Logged: No, IP: 74.222.108.108:61799 } on Server port: 9933 <—> 61799

Okay, the session is created, but nothing happens on the Client side. As you see, the Debug messages are suppose to appear once the function is called when it connects… but it does not call.

Any information will be valuable towards my problem!

@Easelm:

Before Line 55, try add below code line:

smartFox.ThreadSafeMode = true;

Everything should be fine now. More detailed explanation can be found here.