Send datas to client on login

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

Moderators: Lapo, Bax

Skills07
Posts: 103
Joined: 07 Nov 2016, 14:54
Location: Italy

Send datas to client on login

Postby Skills07 » 22 Mar 2017, 14:33

Hello Lapo and friends

i have a new question. The game is going well but now i have to send datas on login to load some info of the player is logged in the app.

i have done this server code that works good and with this i can log a user in the app

Code: Select all

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.smartfoxserver.bitswarm.sessions.ISession;
import com.smartfoxserver.v2.core.ISFSEvent;
import com.smartfoxserver.v2.core.SFSEventParam;
import com.smartfoxserver.v2.db.IDBManager;

import com.smartfoxserver.v2.exceptions.SFSErrorCode;
import com.smartfoxserver.v2.exceptions.SFSErrorData;
import com.smartfoxserver.v2.exceptions.SFSException;
import com.smartfoxserver.v2.exceptions.SFSLoginException;
import com.smartfoxserver.v2.extensions.BaseServerEventHandler;


public class LoginEventHandler extends BaseServerEventHandler {
   @Override
   public void handleServerEvent(ISFSEvent event) throws SFSException {
      // TODO Auto-generated method stub
      trace("Sto chiedendo al server di loggare un utente");
      // Grab parameters from client request
      String userName = (String) event.getParameter(SFSEventParam.LOGIN_NAME);
       String cryptedPass = (String) event.getParameter(SFSEventParam.LOGIN_PASSWORD);
      //String email = (String)event.getParameter(SFSEventParam.LOGIN_PASSWORD);
      ISession session = (ISession) event.getParameter(SFSEventParam.SESSION);
      trace("Username----------" + userName);
      trace("Password----------" + cryptedPass);
      trace("Session----------" + session);
      
      // Get password from DB
      IDBManager dbManager = getParentExtension().getParentZone().getDBManager();
      Connection connection = null;
   

      // Grab a connection from the DBManager connection pool
        try {
         connection = dbManager.getConnection();

         // Build a prepared statement
           PreparedStatement stmt = connection.prepareStatement("SELECT username, password, trofei, gold, gems ,guesswho.users.position, clan_name "
                 + "FROM Users "
                 + "left outer join guesswho.clan on guesswho.users.id_user = guesswho.clan.utente_fondatore "
               + "or guesswho.users.id_user = guesswho.clan.utente_2 "
               + "or guesswho.users.id_user = guesswho.clan.utente_3 "
               + "or guesswho.users.id_user = guesswho.clan.utente_4 "
               + "or guesswho.users.id_user = guesswho.clan.utente_5 "
               + "or guesswho.users.id_user = guesswho.clan.utente_6 "
               + "or guesswho.users.id_user = guesswho.clan.utente_7 "
               + "or guesswho.users.id_user = guesswho.clan.utente_8 "
               + "or guesswho.users.id_user = guesswho.clan.utente_9 "
               + "or guesswho.users.id_user = guesswho.clan.utente_10 "
               + "or guesswho.users.id_user = guesswho.clan.utente_11 "
               + "or guesswho.users.id_user = guesswho.clan.utente_12 "
               + "or guesswho.users.id_user = guesswho.clan.utente_13 "
               + "or guesswho.users.id_user = guesswho.clan.utente_14 "
               + "or guesswho.users.id_user = guesswho.clan.utente_15 "
               + "or guesswho.users.id_user = guesswho.clan.utente_16 "
               + "or guesswho.users.id_user = guesswho.clan.utente_17 "
               + "or guesswho.users.id_user = guesswho.clan.utente_18 "
               + "or guesswho.users.id_user = guesswho.clan.utente_19 "
               + "or guesswho.users.id_user = guesswho.clan.utente_20 "
                 + "where username='"+userName+"'");
          
      
               

          // Execute query
          ResultSet res = stmt.executeQuery();
         // Verify that one record was found
          if (!res.first())
          {
             // This is the part that goes to the client
             SFSErrorData errData = new SFSErrorData(SFSErrorCode.LOGIN_BAD_USERNAME);
             errData.addParameter(userName);
                trace("mi sono intoppato qui???");
             // Sends response if user gave incorrect user name
             throw new SFSLoginException("Bad user name: " + userName, errData);
             
          }
          
          

          String dbPword = res.getString("password");

         // Verify the secure password
         if (!getApi().checkSecurePassword(session, dbPword, cryptedPass))
         {
            trace("mi sono intoppato dopo???");
            SFSErrorData data = new SFSErrorData(SFSErrorCode.LOGIN_BAD_PASSWORD);
            data.addParameter(userName);
            // Sends response if user gave incorrect password
            throw new SFSLoginException("Login failed for user: "  + userName, data);
         }

        }

        // User name was not found
        catch (SQLException e)
        {
           SFSErrorData errData = new SFSErrorData(SFSErrorCode.GENERIC_ERROR);
           errData.addParameter("SQL Error: " + e.getMessage());
           trace("Dio merda mi sono intoppato qui???");
           // Sends response about mysql errors
           throw new SFSLoginException("A SQL Error occurred: " + e.getMessage(), errData);
        }
        finally {
           try{
              connection.close();
           }catch (SQLException e){
              throw new SFSLoginException("A SQL Error occurred: " + e.getMessage());
           }
        }

    }

}



If you see the query i need to send to the client the name of the clan, his number of gold, gems, trophy and username.

How can i send this info??

Thanks for the help
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Send datas to client on login

Postby Lapo » 22 Mar 2017, 15:47

The USER_LOGIN event provides two useful parameters:
LOGIN_IN_DATA
LOGIN_OUT_DATA

With the first you can read custom data sent by the client. With the 2nd you can send custom objects together with the LOGIN event to the client.

Example:

Code: Select all

@Override
public void handleServerEvent(ISFSEvent event) throws SFSException
{
   ISFSObject outData = (ISFSObject) event.getParameter(SFSEventParam.LOGIN_OUT_DATA);
   
   // Add data to the object
   outData.putInt("number", 100);
   ...
   ...
}

You just need to populate the object, without calling anything else.

On the client side you will receive the object in the SFSEvent.LOGIN event, with parameter name "data"

cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
Skills07
Posts: 103
Joined: 07 Nov 2016, 14:54
Location: Italy

Re: Send datas to client on login

Postby Skills07 » 23 Mar 2017, 12:18

Lapo i have done this

Code: Select all

trace("Sto chiedendo al server di loggare un utente");
      // Grab parameters from client request
      String userName = (String) event.getParameter(SFSEventParam.LOGIN_NAME);
       String cryptedPass = (String) event.getParameter(SFSEventParam.LOGIN_PASSWORD);
      //String email = (String)event.getParameter(SFSEventParam.LOGIN_PASSWORD);
      ISession session = (ISession) event.getParameter(SFSEventParam.SESSION);
      trace("Username----------" + userName);
      trace("Password----------" + cryptedPass);
      trace("Session----------" + session);
      
       ISFSObject outData = (ISFSObject) event.getParameter(SFSEventParam.LOGIN_OUT_DATA);
      
      
        ResultSet res = stmt.executeQuery();
         
          while(res.next())
         {

            String user = res.getString("username");
            trace(user);
            int trofei = res.getInt("trofei");
            trace(trofei);
            
            int gold = res.getInt("gold");
            trace(gold);
            int gems = res.getInt("gems");
            trace(gems);
            String position = res.getString("position");
            
            if (position == null){
               position = "";
            }
            trace(position);
            
            String clan_name = res.getString("clan_name");
            
            if (clan_name == null){
               clan_name = "noclan";
            }
            trace(clan_name);
            
            outData.putUtfString("nome_utente", user);
            outData.putInt("trofei", trofei);
            outData.putInt("gold", gold);
            outData.putInt("gems", gems);
            outData.putUtfString("position", position);
            outData.putUtfString("clan_name", clan_name);



if is correct my collaborator how can he read this object???
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Send datas to client on login

Postby Lapo » 23 Mar 2017, 14:35

What platform is your client?
Lapo

--

gotoAndPlay()

...addicted to flash games
Skills07
Posts: 103
Joined: 07 Nov 2016, 14:54
Location: Italy

Re: Send datas to client on login

Postby Skills07 » 23 Mar 2017, 14:54

Unity c#.

My collaborator writes c# code of unity, i'm only writing server code in Java.

Is it correct my code?

What is missing con unity client side?
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Send datas to client on login

Postby Lapo » 23 Mar 2017, 15:26

The code looks good, but only compiling and running it will give you the final verdict :)

As I mentioned the data is received in the LOGIN event with parameter name of "data".
So...

Code: Select all

SFSObject myData = (SFSObject) evt.Params["data"];


cheers
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 96 guests