Main Menu  

   

Login Form  

   

(011) Login Assistant Component

Details
ZoneExtension.java
package LoginExt;

import com.smartfoxserver.v2.components.login.LoginAssistantComponent;
import com.smartfoxserver.v2.extensions.SFSExtension;

public class ZoneExtension extends SFSExtension{

	private LoginAssistantComponent lac;

	@Override
	public void init()
	{
		lac = new LoginAssistantComponent(this);

		lac.getConfig().loginTable = "users";
		lac.getConfig().userNameField = "username";
		lac.getConfig().passwordField = "password";
	}

	@Override
	public void destroy()
	{
		super.destroy();
	}
}
Login.cs
using UnityEngine;
using System.Collections;
using Sfs2X;
using Sfs2X.Core;
using Sfs2X.Requests;

public class Login : MonoBehaviour {

	public string ServerIP = "127.0.0.1";
	public int ServerPort = 9933;
	public string ZoneName = "BasicExamples";
	public string UserName = "kermit";
	public string Password = "thefrog";
	
	SmartFox sfs;

	void Start()
	{
		sfs = new SmartFox();
		sfs.ThreadSageMode = true;
		
		sfs.AddEventListener(SFSEvent.CONNECTION, OnConnection);
		sfs.AddEventListener(SFSEvent.LOGIN, OnLogin);
		sfs.AddEventListener(SFSEvent.LOGIN_ERROR, OnLoginError);
		
		sfs.Connect(ServerIP, ServerPort);
	}
	
	void OnConnection(BaseEvent e)
	{
		if ((bool)e.Params["success"])
		{
			Debug.Log("Successfully Connected!");
			sfs.Send(new LoginRequest(UserName, Password, ZoneName));
		} else {
			Debug.Log("Connection Failed!");
		}
	}
	
	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 Update()
    {
        sfs.ProcessEvents();
    }
     
    void OnApplicationQuit()
    {
        if (sfs.IsConnected)
            sfs.Disconnect();
    }
}

* Special thanks to Lannister for pointing out a typo in the above code!
   
© 2004-2015 Genesis Rage