Main Menu  

   

Login Form  

   

(003) Optional Config File

Details
sfs-config.xml
<SmartFoxConfig>
	<ip>127.0.0.1</ip>
	<port>9933</port>
	<udpIp>127.0.0.1</udpIp>
	<udpPort>9933</udpPort>
	<zone>BasicExamples</zone>
</SmartFoxConfig>
SFS_Connect.cs
using UnityEngine;
using System.Collections;
using Sfs2X;
using Sfs2X.Core;
using Sfs2X.Requests;

public class SFS2X_Connect : MonoBehaviour {
	
	public string ConfigFile = "_Scenes/Recoding/sfs-config.xml";
	public bool UseConfigFile = false;
	public string ServerIP = "127.0.0.1";
	public int ServerPort = 9933;
	public string ZoneName = "BasicExamples";
	public string UserName = "";
	
	SmartFox sfs;

	void Start()
	{
		sfs = new SmartFox();
		sfs.ThreadSafeMode = true;
		
		sfs.AddEventListener(SFSEvent.CONNECTION, OnConnection);
		sfs.AddEventListener(SFSEvent.LOGIN, OnLogin);
		sfs.AddEventListener(SFSEvent.LOGIN_ERROR, OnLoginError);
		sfs.AddEventListener(SFSEvent.CONFIG_LOAD_SUCCESS, OnConfigLoad);
		sfs.AddEventListener(SFSEvent.CONFIG_LOAD_FAILURE, OnConfigFail);
		
		if (UseConfigFile)
		{
			sfs.LoadConfig(Application.dataPath + "/" + ConfigFile);
		} else {
			sfs.Connect(ServerIP, ServerPort);
		}
	}
	
	void OnConfigLoad(BaseEvent e)
	{
		Debug.Log("Config File Loaded");
		sfs.Connect(sfs.Config.Host, sfs.Config.Port);
	}
	
	void OnConfigFail(BaseEvent e)
	{
		Debug.Log("Failed to load Config File");
	}
	
	void OnLogin(BaseEvent e)
	{
		Debug.Log("Logged In: " + e.Params["user"]);
	}
	
	void OnLoginError(BaseEvent e)
	{
		Debug.Log("Login error: (" + e.Params["errorCode"] + "): " + e.Params["errorMessage"]);
	}
	
	void OnConnection(BaseEvent e)
	{
		if ((bool)e.Params["success"])
		{
			Debug.Log("Successfully Connected");
			if (UseConfigFile)
				ZoneName = sfs.Config.Zone;
			sfs.Send(new LoginRequest(UserName, "", ZoneName));
		} else {
			Debug.Log("Connection Failed");
		}
	}
	
	void Update()
	{
		sfs.ProcessEvents();
	}
	
	void OnApplicationQuit()
	{
		if (sfs.IsConnected)
			sfs.Disconnect();
	}
}
   
© 2004-2015 Genesis Rage