I am having an issue with unity and unregistering the callback delegates in my custom login script. I have created a script called LoginGui.js which contains the following:
Code: Select all
import System;
import SmartFoxClientAPI;
import SmartFoxClientAPI.Data;
import SmartFoxClientAPI.Util;
import SmartFoxClientAPI.Http;
var smartFox : SmartFoxClient = new SmartFoxClient();
var SmartFox : HttpConnection;
var shuttingDown = false;
var serverIP : String = "myip";
var serverPort : String = "myport";
public var zone : String = "simpleChat";
public var debug : boolean = true;
public var gskin : GUISkin;
var username : String = "";
var pass : String = "";
var loginErrorMessage : String = "";
var connectionAttempt : boolean = false;
/************
* Unity callback methods
************/
function OnApplicationQuit() {
shuttingDown = true;
}
function Awake()
{
Application.runInBackground = true;
smartFox.Connect(serverIP, Convert.ToInt32(serverPort));
// Register callback delegate
SFSEvent.onConnection = OnConnection;
SFSEvent.onConnectionLost = OnConnectionLost;
SFSEvent.onLogin = OnLogin;
SFSEvent.onRoomListUpdate = OnRoomList;
SFSEvent.onDebugMessage = OnDebugMessage;
SFSEvent.onExtensionResponse = onExtensionResponse;
}
function FixedUpdate()
{
smartFox.ProcessEventQueue();
}
function OnGUI()
{
GUI.skin = gskin;
GUI.Label(new Rect(2, -2, 680, 70), "");
if (smartFox.IsConnected())
{
// Login
GUI.Label(new Rect(10, 116, 100, 100), "Username: ");
username = GUI.TextField(new Rect(100, 116, 200, 20), username, 25);
GUI.Label(new Rect(10, 136, 100, 100), "Password: ");
pass = GUI.TextField(new Rect(100, 136, 200, 20), pass, 25);
GUI.Label(new Rect(10, 218, 100, 100), loginErrorMessage);
if (GUI.Button(new Rect(100, 166, 100, 24), "Login") || (Event.current.type == EventType.keyDown && Event.current.character == "\n"))
{
smartFox.Login(zone, username, pass);
}
} else {
GUI.Label(new Rect(10, 150, 100, 100), "Waiting for connection");
GUI.Label(new Rect(10, 218, 100, 100), loginErrorMessage);
}
}
function UnregisterSFSSceneCallbacks() {
// This should be called when switching scenes, so callbacks from the backend do not trigger code in this scene
//SFSEvent.onConnection = null;
//SFSEvent.onConnectionLost = null;
//SFSEvent.onLogin = null;
//SFSEvent.onRoomListUpdate = null;
//SFSEvent.onDebugMessage = null;
//SFSEvent.onExtensionResponse = null;
//SFSEvent.onConnection = onConnection;
//SFSEvent.onConnectionLost -= OnConnectionLost;
//SFSEvent.onLogin -= OnLogin;
//SFSEvent.onRoomListUpdate -= OnRoomList;
//SFSEvent.onDebugMessage -= OnDebugMessage;
//SFSEvent.onExtensionResponse -= onExtensionResponse;
}
/************
* Callbacks from the SFS API
************/
function OnConnection(success : boolean, error : String)
{
if ( success ) {
Debug.Log("Connected to server...");
} else {
loginErrorMessage = error;
}
}
function OnConnectionLost() {
loginErrorMessage = "Connection lost / no connection to server";
}
function OnDebugMessage(message : String)
{
Debug.Log("[SFS DEBUG] " + message);
}
function OnLogin(success : boolean, name : String , error : String)
{
if ( success )
{
Debug.Log("User: " + name + " has logged in");
} else
{
loginErrorMessage = error;
}
}
function OnRoomList(roomList : Hashtable)
{
Debug.Log("Onroomlist Event called");
UnregisterSFSSceneCallbacks();
Application.LoadLevel("Port Alexandria");
}
function onExtensionResponse(evt : SFSObject)
{
Debug.Log("RECIEVED RESPONCE");
var dataObject : SFSObject = evt;
var cmd : String = dataObject.GetString("_cmd") ;
var id : String = dataObject.GetString("id") ;
var username : String = dataObject.GetString("name") ;
Debug.Log("ID: " + id);
Debug.Log("username: " + username);
Debug.Log("CMD: " + cmd);
if (cmd == "logOK")
{
myUserId = Convert.ToInt32(id);
myUserName = username;
Debug.Log("Login successfull");
}
else if (cmd == "logKO")
{
Debug.Log("Login unsuccessfull");
}
}
function Update ()
{
}
I have also created a java extension that is used to validate the credentials im the mysqldatabase. Everything is fine except that idoes not load the level because i can not figured out how to unregister the callback delegates in a js script. I have looked through the forums and have had no luck finding the answer. Can anyone help? Any suggestions would be appreciated.
Thanks