http://docs2x.smartfoxserver.com/ExtensionsJava/signup-assistant-basics
But when I test it's not working for me,I can't insert new user to database although connecting to sqlserver Database is no problem.
Could someone help me please? thank you very much (my email : esondinh@gmail.com).
Bellow is my code from Server side and Client side:
1. Server side
Code: Select all
import com.smartfoxserver.v2.extensions.SFSExtension;
import com.smartfoxserver.v2.components.login.ILoginAssistantPlugin;
import com.smartfoxserver.v2.components.login.LoginData;
import com.smartfoxserver.v2.components.login.LoginAssistantComponent;
import com.smartfoxserver.v2.components.signup.SignUpAssistantComponent;
import com.smartfoxserver.v2.security.DefaultPermissionProfile;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import java.util.Arrays;
public class SignUpExtension extends SFSExtension {
private SignUpAssistantComponent suac;
@Override
public void init() {
trace("Init SIGNUP extension");
suac = new SignUpAssistantComponent();
suac.getConfig().signUpTable = "Account";
suac.getConfig().usernameField = "AccName";
suac.getConfig().passwordField = "AccPass";
suac.getConfig().emailField = "Email";
addRequestHandler(SignUpAssistantComponent.COMMAND_PREFIX, suac);
}
public void destroy() {
super.destroy();
}
}
2. Client side
Code: Select all
using UnityEngine;
using Sfs2X;
using Sfs2X.Core;
using Sfs2X.Requests;
using Sfs2X.Entities.Data;
public class TutorialRegistration : MonoBehaviour
{
public string userName = "";
public string userPass = "";
public string userEmail = "testSFS@gmail.com";
public string zoneName = "SignUp";
public SmartFox sfs;
public string ServerIP = "127.0.0.1";
public int ServerPort = 9933;
private string CMD_SUBMIT = "$SignUp.Submit";
public bool isConnect = false;
public void Start()
{
sfs = new SmartFox();
sfs.ThreadSafeMode = true;
sfs.AddEventListener(SFSEvent.CONNECTION, onConnection);
sfs.AddEventListener(SFSEvent.CONNECTION_LOST, onConnectionLost);
sfs.AddEventListener(SFSEvent.LOGIN, onLogin);
sfs.AddEventListener(SFSEvent.LOGIN_ERROR, onLoginError);
sfs.AddEventListener(SFSEvent.EXTENSION_RESPONSE, onExtensionResponse);
sfs.Connect(ServerIP, ServerPort);
}
//smartFoxServer connection handler
private void onConnection(BaseEvent e)
{
if ((bool)e.Params["success"])
{
Debug.Log("succesfully connected to smartfoxserver 2x");
isConnect = true;
}
else
{
Debug.Log("Connection Failure:" + (string)e.Params["errorMessage"]);
isConnect = false;
}
}
// SmartFoxServer ConnectionLost handler
private void onConnectionLost(BaseEvent e)
{
Debug.Log("Connection was lost. Reason: " + (string)e.Params["reason"]);
isConnect = false;
}
// HANDLES login success event
private void onLogin(BaseEvent e)
{
//Check connection
if (isConnect)
{
if ((bool)e.Params["success"])
{
Debug.Log("succesfully loggin to smartfoxserver 2x");
// Login as Guest to SignUp zone
sfs.Send(new LoginRequest("", "", zoneName));
//Send user register data to Server
ISFSObject objOut = new SFSObject();
objOut.PutUtfString("name", userName);
objOut.PutUtfString("password", userPass);
objOut.PutUtfString("email", userEmail);
sfs.Send(new ExtensionRequest(CMD_SUBMIT, objOut));
}
else
{
Debug.Log("loggin Failure:" + (string)e.Params["errorMessage"]);
}
}
else
{
Debug.Log("Can't connect to smartfoxserver 2x");
}
}
// Handles login error event
private void onLoginError(BaseEvent e)
{
Debug.Log("Login Failure: " + (string)e.Params["errorMessage"]);
}
// Handles extension response from the server
private void onExtensionResponse(BaseEvent e)
{
bool success = (bool)e.Params["success"];
ISFSObject s = (SFSObject)e.Params["params"];
if (s.ContainsKey("success"))
{
Debug.Log(s.GetUtfString("success"));
sfs.Send(new LogoutRequest());
}
if (s.ContainsKey("failure"))
{
Debug.Log(s.GetUtfString("failure"));
sfs.Send(new LogoutRequest());
}
if (s.ContainsKey("error"))
{
Debug.Log(s.GetUtfString("error"));
sfs.Send(new LogoutRequest());
}
}
void Update()
{
if (sfs != null)
sfs.ProcessEvents();
}
// Handle disconnection automagically
// ** Important for Windows users - can cause crashes otherwise
void OnApplicationQuit()
{
if (sfs != null && sfs.IsConnected)
{
sfs.Disconnect();
}
}
// Disconnect from the socket when ordered by the main Panel scene
// ** Important for Windows users - can cause crashes otherwise
public void Disconnect()
{
OnApplicationQuit();
}
}