i cant get it to work
here is my code:
Code: Select all
using UnityEngine;
using System;
using System.Collections;
using SmartFoxClientAPI;
using SmartFoxClientAPI.Data;
using SmartFoxClientAPI.Util;
public class LoginGUI : MonoBehaviour {
private SmartFoxClient smartFox;
private bool shuttingDown = false;
public string serverIP = "127.0.0.1";
public int serverPort = 3000;
public string zone = "simpleChat";
public bool debug = true;
public string log;
public GUISkin gSkin;
private string nick = "";
private string pword = "";
private string loginErrorMessage = "";
/************
* Unity callback methods
************/
void OnApplicationQuit() {
shuttingDown = true;
}
void FixedUpdate() {
smartFox.ProcessEventQueue();
}
void Awake() {
Application.runInBackground = true;
if ( SmartFox.IsInitialized() ) {
smartFox = SmartFox.Connection;
} else {
try {
smartFox = new SmartFoxClient(debug);
smartFox.runInQueueMode=true;
} catch ( Exception e ) {
loginErrorMessage = e.ToString();
}
}
// Register callback delegate
SFSEvent.onConnection += OnConnection;
SFSEvent.onConnectionLost += OnConnectionLost;
SFSEvent.onLogin += OnLogin;
SFSEvent.onRoomListUpdate += OnRoomList;
SFSEvent.onDebugMessage += OnDebugMessage;
//SFSEvent.onExtensionResponse += OnExtensionResponse;
SFSEvent.onExtensionResponse += OnExtensionResponse;
SFSEvent.onExtensionResponse -= OnExtensionResponse;
smartFox.Connect(serverIP, serverPort);
}
void OnGUI() {
GUI.matrix = Matrix4x4.TRS (Vector3.zero, Quaternion.identity, new Vector3 (Screen.width / 600.0f, Screen.height / 400.0f, 1f));
GUI.skin = gSkin;
//GUI.Label(new Rect(2, -2, 680, 70), "", "SFSLogo");
GUI.Box(new Rect (125,50,400,300), "Login");
if ( smartFox.IsConnected() ) {
// Login
GUI.Label(new Rect(180, 117, 65, 100), "Username: ");
nick = GUI.TextField(new Rect(250, 117, 150, 20), nick, 25);
GUI.Label(new Rect(180, 145, 150, 20), "Password: ");
pword = GUI.PasswordField(new Rect(250, 145, 150, 20), pword, "*"[0], 25);
GUI.Label(new Rect(10, 218, 400, 100), loginErrorMessage);
if ( GUI.Button(new Rect(300, 230, 100, 20), "Login") || (Event.current.type == EventType.keyDown && Event.current.character == '\n')) {
smartFox.Login(zone, nick, pword);
}
} else {
GUI.Label(new Rect(10, 150, 400, 100), "Waiting for connection");
GUI.Label(new Rect(10, 218, 400, 100), loginErrorMessage);
}
}
/************
* Helper methods
************/
private void UnregisterSFSSceneCallbacks() {
// This should be called when switching scenes, so callbacks from the backend do not trigger code in this scene
SFSEvent.onConnection -= OnConnection;
SFSEvent.onConnectionLost -= OnConnectionLost;
SFSEvent.onLogin -= OnLogin;
SFSEvent.onRoomListUpdate -= OnRoomList;
SFSEvent.onDebugMessage -= OnDebugMessage;
SFSEvent.onExtensionResponse -= OnExtensionResponse;
SFSEvent.onExtensionResponse += OnExtensionResponse;
}
/************
* Callbacks from the SFS API
************/
void OnConnection(bool success, string error) {
if ( success ) {
SmartFox.Connection = smartFox;
} else {
loginErrorMessage = error;
}
}
void OnConnectionLost() {
loginErrorMessage = "Connection lost / no connection to server";
}
public void OnDebugMessage(string message) {
Debug.Log("[SFS DEBUG] " + message);
}
public void OnExtensionResponse(object data, string type) {
if (type == SmartFoxClient.XTMSG_TYPE_XML)
{
SFSObject responseData = (SFSObject)data;
if(responseData.GetString("_cmd") == null)
{
loginErrorMessage = "Invalid login";
}
switch ( responseData.GetString("_cmd") )
{
case "logOK":
OnLogin(true, responseData.GetString("name"), responseData.GetString("err"));
break;
case "logKO":
loginErrorMessage = "Invalid username or password!";
break;
}
}
}
public void OnLogin(bool success, string name, string error) {
if ( success ) {
// Lets wait for the room list
} else {
// Login failed - lets display the error message sent to us
loginErrorMessage = error;
}
}
void OnRoomList(Hashtable roomList) {
// When room list is updated we are ready to move on to the lobby
UnregisterSFSSceneCallbacks();
Application.LoadLevel("lobby");
}
}
can anyone help me???
thanks
Robert