Here is the server code:
Code: Select all
import com.smartfoxserver.v2.components.login.LoginAssistantComponent;
import com.smartfoxserver.v2.components.signup.SignUpAssistantComponent;
import com.smartfoxserver.v2.components.signup.SignUpConfiguration;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.extensions.SFSExtension;
public class SignUpTestExtension extends SFSExtension
{
private SignUpAssistantComponent suac;
private LoginAssistantComponent lac;
@Override
public void init()
{
initSignUpAssistant();
initLoginAssistant();
}
@Override
public void destroy()
{
super.destroy();
lac.destroy();
}
private void initSignUpAssistant()
{
suac = new SignUpAssistantComponent();
suac.getConfig().signUpTable = "users";
addRequestHandler(SignUpAssistantComponent.COMMAND_PREFIX, suac);
}
private void initLoginAssistant()
{
lac = new LoginAssistantComponent(this);
lac.getConfig().loginTable = "users";
};
}
}
And the client code:
Code: Select all
using UnityEngine;
using System.Collections;
using Sfs2X;
using Sfs2X.Core;
using Sfs2X.Requests;
using Sfs2X.Entities.Data;
public class SignUp : MonoBehaviour {
public string ServerIP = "127.0.0.1";
public int ServerPort = 9933;
public string ZoneName = "BasicExamples";
public string UserName = "";
SmartFox sfs;
string CMD_Signup = "$SignUp.Submit";
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.EXTENSION_RESPONSE, OnExtensionResponse);
sfs.Connect(ServerIP, ServerPort);
}
void OnConnection(BaseEvent e)
{
if ((bool)e.Params["success"])
{
Debug.Log("Successfully Connected!");
sfs.Send(new LoginRequest("", "", ZoneName));
} else {
Debug.Log("Connection Failed!");
}
}
void OnLogin(BaseEvent e)
{
Debug.Log("Logged In: " + e.Params["user"]);
ISFSObject objOut = new SFSObject();
objOut.PutUtfString("nickname", UserName);
sfs.Send(new ExtensionRequest(CMD_Signup, objOut));
}
void OnLoginError(BaseEvent e)
{
Debug.Log("Login error: (" + e.Params["errorCode"] + "): " + e.Params["errorMessage"]);
}
void OnExtensionResponse(BaseEvent e)
{
string cmd = (string)e.Params["cmd"];
ISFSObject objIn = (SFSObject)e.Params["params"];
if (cmd == CMD_Signup)
{
if (objIn.ContainsKey("errorMessage"))
{
Debug.Log("Signup Error: " + objIn.GetUtfString("errorMessage"));
}
else if (objIn.ContainsKey("success"))
{
Debug.Log("Signup Successful");
}
}
}
void FixedUpdate()
{
sfs.ProcessEvents();
}
void OnApplicationQuit()
{
if (sfs.IsConnected)
sfs.Disconnect();
}
}