Request database Return Empty Array

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

Moderators: Lapo, Bax

mers
Posts: 16
Joined: 20 Dec 2017, 16:17

Request database Return Empty Array

Postby mers » 26 Oct 2022, 12:49

Hello, i work on Unity and try get data from database linked to smartfox but i receive empty array.
My Extension is drop into folder extension and is loaded on start SFS2X without problem

Server Side :

Extension Global

Code: Select all

package eod.extension;

import com.smartfoxserver.v2.extensions.SFSExtension;

public class EODClassicExtension extends SFSExtension
{
   @Override
   public void init()
   {
      trace("EOD Classic Extension Loaded...");
      
      addRequestHandler("get_player_data", PlayerData.class);
   }
}


The extension Request

Code: Select all

public class PlayerData extends BaseClientRequestHandler
{
   public void init()
   {
      trace("Player Data Initialised");
   }
   
    @Override
    public void handleClientRequest(User sender, ISFSObject params)
    {
        IDBManager dbManager = getParentExtension().getParentZone().getDBManager();
        String sql = "SELECT * FROM users";
       
        trace("Get Player Data Request From : " + sender + " database : "+ dbManager.toString());
       
        try
        {
            // Obtain a result set
            ISFSArray res = dbManager.executeQuery(sql, new Object[] {});
             
            // Populate the response parameters
            ISFSObject response = new SFSObject();
            response.putSFSArray("users", res);
             
            // Send back to requester
            send("get_player_data", response, sender);
        }
        catch (SQLException e)
        {
            trace("SQL Failed: " + e.toString());
        }
    }
}


DataBase

Base : EOD
Table : users
In Table ( id, steam_id, steam_name ) i fill field for test

Client Side :

Code: Select all

private void OnExtensionResponse(BaseEvent evt)
   {
      Debug.Log("Extension Response Receive");
      string cmd = (string)evt.Params["cmd"];
      SFSObject sfso = (SFSObject)evt.Params["params"];

      if (cmd == "get_player_data")
      {
         Debug.Log(sfso.GetSFSArray("users").ToString());
         Debug.Log("SignUp error:" + sfso.GetUtfString("steam_id"));

         //if (sfso.GetBool("success"))
         //   Debug.Log("Success, thanks for registering");
         //else
         //   Debug.Log("SignUp error:" + (string)evt.Params["errorMessage"]);
      }
   }
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Request database Return Empty Array

Postby Lapo » 26 Oct 2022, 15:22

Hi,
the code looks fine. If there are no server side errors and the result of the query is empty I'd expect it to be what's in the DB's table.

To debug this you can just log or print out the SFSArray returned by the query like this:

Code: Select all

trace(res.getDump());


Let us know
Lapo
--
gotoAndPlay()
...addicted to flash games
mers
Posts: 16
Joined: 20 Dec 2017, 16:17

Re: Request database Return Empty Array

Postby mers » 26 Oct 2022, 16:26

thanks for the answer, so i added the trace command and on the sfs console i have no problem, i can see my fields fine

Code: Select all

18:32:54,372 INFO  [SFSWorker:Ext:2] Extensions     - {EOD}:
    (sfs_object)
      (utf_string) steam_id: teststeamid
      (int) id: 1
      (utf_string) steam_name: cmoi
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Request database Return Empty Array

Postby Lapo » 26 Oct 2022, 16:36

You can do the same with the object that you get on the client side.
Try outputting the "Params" object in the Unity console:

Code: Select all

Debug.Log(sfso.GetDump());


Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
mers
Posts: 16
Joined: 20 Dec 2017, 16:17

Re: Request database Return Empty Array

Postby mers » 26 Oct 2022, 16:51

work too on client side

Code: Select all

(sfs_array) users:
      (sfs_object)
         (utf_string) steam_id: teststeamid
         (int) id: 1
         (utf_string) steam_name: cmoi
      
mers
Posts: 16
Joined: 20 Dec 2017, 16:17

Re: Request database Return Empty Array

Postby mers » 27 Oct 2022, 01:11

I use this approach to read the data and it works

Code: Select all

 ISFSObject _params = (ISFSObject)evt.Params["params"];
         ISFSArray _users = _params.GetSFSArray("users");

         for (int i = 0; i < _users.Size(); i++)
         {
            ISFSObject _user = _users.GetSFSObject(i);
            _user.GetUtfString("steam_id");

            Debug.Log("steam id = " + _user.GetUtfString("steam_id"));
         }
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Request database Return Empty Array

Postby Lapo » 27 Oct 2022, 07:56

Correct.
The array can contain any number of database "rows", each represented by an SFSObject.

Sorry it didn't come to mind earlier.
Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
mers
Posts: 16
Joined: 20 Dec 2017, 16:17

Re: Request database Return Empty Array

Postby mers » 27 Oct 2022, 10:16

it doesn't matter you helped me a lot I had forgotten the fundamentals a bit.

I have a somewhat strange question, it is possible that on the local side in a method I send the sender() and that this return me directly the response ? for example data = sender(blabla)
to check the response directly without going through the OnExtensionResponse method?
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Request database Return Empty Array

Postby Lapo » 27 Oct 2022, 10:22

No, all communication is done asynchronously: you send a message and eventually you'll get a response from the other side.
This way the traffic can scale to very high volumes. With synchronous communication the server would be forced to keep threads busy until each transaction is completed which kills scalability.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
mers
Posts: 16
Joined: 20 Dec 2017, 16:17

Re: Request database Return Empty Array

Postby mers » 27 Oct 2022, 10:44

That's what I thought, thanks for confirming

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 55 guests