How To Create User Registration In SFS2X and Save Data To SqlServer Database

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

User avatar
esondinh
Posts: 5
Joined: 28 May 2021, 06:37

How To Create User Registration In SFS2X and Save Data To SqlServer Database

Postby esondinh » 17 Jun 2021, 03:18

Hi everyone,I'm new member of forum and now I have a problem with "Sign Up Assistant" in Sfs2x,I followed this tutorial:
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();
    }
}
Best regards,
sakura (Mr.)
Email : esondinh@gmail.com
User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: How To Create User Registration In SFS2X and Save Data To SqlServer Database

Postby Lapo » 17 Jun 2021, 05:43

HI,
have you checked the server side logs after trying to register a new User? You should find some error giving you more details about what has happened.

Let us know
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
esondinh
Posts: 5
Joined: 28 May 2021, 06:37

Re: How To Create User Registration In SFS2X and Save Data To SqlServer Database

Postby esondinh » 17 Jun 2021, 10:53

Thanks for your reply Lapo.
I check my logs but I can't found exception,could you check souce code help me,please?
I attach source on this link :
https://drive.google.com/file/d/1THfmNM ... sp=sharing
Best regards,
sakura (Mr.)
Email : esondinh@gmail.com
User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: How To Create User Registration In SFS2X and Save Data To SqlServer Database

Postby Lapo » 17 Jun 2021, 11:36

Hi,
one problem I see in your client side logic is that you're sending both the LoginRequest and the next ExtensionRequest at the same time, without waiting for the client to actually triggering an SFSEvent.LOGIN.

My suggestion is to only send the LoginRequest and then move the Extension request in the SFSEvent.LOGIN handler.

This should fix it.
Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
esondinh
Posts: 5
Joined: 28 May 2021, 06:37

Re: How To Create User Registration In SFS2X and Save Data To SqlServer Database

Postby esondinh » 21 Jun 2021, 03:55

Sorry Lapo , few days ago i was busy.
I followed your recommendation and it worked fine for me,thanks a lot and i will share my solution for this problem,hope this share will be helpful for everyone.
There are some steps we need to do register new an user and save it to database.
1- Call SmartFox.connect()
2- Wait for the SFSEvent.CONNECTION event to be invoked with parameter success = true,
after the successful connected,we need login as Guest to zone on the onConnection event (Note : set username and password are empty)

Code: Select all

sfs.Send(new LoginRequest("", "", zoneName));

3- From onLogin event -> we will send user register data to Server

Code: Select all

 ISFSObject objOut = new SFSObject();
        string userName = "Ronaldo";
        string  userPass = "1234567"; // please set password length > 6
        string  userEmail = "RonaldoSFS@gmail.com";
        objOut.PutUtfString("AccName", userName);
        objOut.PutUtfString("AccPass", userPass);
        objOut.PutUtfString("Email", userEmail);
        Debug.Log("AccName: " + userName);
        Debug.Log("AccPass: " + userPass);
        Debug.Log("Email: " + userEmail);
        sfs.Send(new ExtensionRequest(CMD_SUBMIT, objOut));


Note : You don't need setup a custom login in the admin tool for your zone where you register users. They can just login as guests.
Best regards,
sakura (Mr.)
Email : esondinh@gmail.com

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 42 guests