OnExtensionResponse - Not Firing.

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

Moderators: Lapo, Bax

Sanity17
Posts: 4
Joined: 27 Apr 2014, 23:51

OnExtensionResponse - Not Firing.

Postby Sanity17 » 17 Apr 2017, 17:38

Hello All. I am testing SFS2x and successfully following tutorials got the server to Log into a database etc.
It can read custom tables and return the data set. Compile into a ISFS Object and sends to the Client that requested the request.

How ever the client doesn't receive or process the request.
There is no server error. In admin tool i can see send/receive increasing when the request is sent.

The request from client is:

public void test(){
ISFSObject objOut = new SFSObject();
objOut.PutUtfString("Target", Target.text);
sfs.Send(new ExtensionRequest("Char", objOut));
Debug.Log ("Request Sent");
}

Taking Target as an selected gameObject (For testing it is string defined).

Server then handles the request via MySql Statements:

Statement stmt = con.createStatement();
String sql = "SELECT *" + "FROM `character`" + "WHERE `charname` = '" + Target + "'";
ResultSet rs = stmt.executeQuery(sql);
System.out.println("Executing query");

Collects 16 sets of data.

Makes new ISFSObject:
ISFSObject objOut = new SFSObject();
objOut.putInt("ID", rs.getInt("ID"));

And sends the data back.

ZoneExtension parentEx = (ZoneExtension) getParentExtension();
con.isClosed();
System.out.println("Database Connection Closed.");
parentEx.send("Char", objOut, user);

Making sure to close the connection before sending.

So back to the local client is listening for:
sfs.AddEventListener(SFSEvent.EXTENSION_RESPONSE, ExtResponse);

void ExtResponse(BaseEvent e){
ISFSObject objIn = (SFSObject)e.Params["params"];
string cmd = (string)e.Params["cmd"];

if (cmd == "Char")
{
Debug.Log("Name: " + objIn.GetUtfString("charname"));

} else {
Debug.Log ("Failure: Unknown Data Package.");
}
}

This event is never fired. No error on server.
The server log shows as:

Target received
Attempting to Connect to database
Connected to Database
Executing query
ID: 1
Charname: Kermit
...etc...
Database Connection Closed.
Char data sent to( User Name: Bobby, Id: 1, Priv: 0, Sess: 127.0.0.1 )
[SFSObject, size: 16]

I've double checked the server. Client is active, kept alive via PING_PONG. So im lost on why this is never called.

Using SFS 2.12.5
This is all tested on the same machine.
Any help appreciated as checked multiple documentation and cannot resolve this.
User avatar
meanvel
Posts: 129
Joined: 19 Jan 2017, 14:06

Re: OnExtensionResponse - Not Firing.

Postby meanvel » 18 Apr 2017, 06:18

Sanity17 wrote:How ever the client doesn't receive or process the request.
There is no server error. In admin tool i can see send/receive increasing when the request is sent.

ZoneExtension parentEx = (ZoneExtension) getParentExtension();
con.isClosed();
System.out.println("Database Connection Closed.");
parentEx.send("Char", objOut, user);

Making sure to close the connection before sending.

So back to the local client is listening for:
sfs.AddEventListener(SFSEvent.EXTENSION_RESPONSE, ExtResponse);



You serverside code is different... Here is a example of how I send data from server to client. I just use the Api. Not exactly sure what your doing. This class doesn't even appear to exist in the smartFox serverside java API... "ZoneExtension" At least, my IDE can't pull it up.

The API should be available for any class which extends SFSExtension.

Code: Select all

} else if (cmdName == extensionConstants.GET_GAME_TICK) {
            User targetUser = sfsApi.getUserById(newParams.getInt("id"));
            newParams.putByte("t",(byte) 50);
            newParams.putInt("gt",gameTick);
            List<User> targetUsers = new LinkedList<User>();
            targetUsers.add(targetUser);
            sfsApi.sendObjectMessage(mapRoom,targetUser,newParams,targetUsers);

            return true;
        }


Alternatively, you could use sfsApi.sendExtensionResponse. False is for "Use UDP".

Code: Select all

sfsApi.sendExtensionResponse(extensionConstants.GET_GAME_TICK, newParams, targetUser, mapRoom, false);


Docs...
http://docs2x.smartfoxserver.com/api-do ... nsion.html

Also, tip...
System.out.println("Database Connection Closed.");
Just use trace(ExtensionLogLevel.INFO,"Database Connection Closed.");. The trace function is available in any class which extends the SFSExtension class. The serverside trace() function writes to smartfox.log. You can access it manually or with the admin tool.
Sanity17
Posts: 4
Joined: 27 Apr 2014, 23:51

Re: OnExtensionResponse - Not Firing.

Postby Sanity17 » 18 Apr 2017, 19:40

Sorry for it being different i was following the code on: https://www.youtube.com/watch?v=nKGxhwJ0Ccc

public void handleClientRequest(User user, ISFSObject objIn){
// Connects to sqlDB..................... ^
//Extracts data............................|
//puts into new object ...................|
....................send("Char", objOut, user);

Above is the main break down and im trying to pass the sender as the user to retrieve the data thats been collected.

If the main parameter on the request is(User user, ISFSObject objIn)
All im trying to do is talk directly back to the user thats requested the information.

Im following this from: https://youtu.be/Y7BLoikCl-g?t=3m54s

Tad outdated i confess

Chaing the send to:

List<User> targetUsers = new LinkedList<>();
User targetUser = user;
targetUsers.add(targetUser);
send("Char", objOut, targetUsers);

Still isnt recieved by the Unity instance or build instance.
User avatar
meanvel
Posts: 129
Joined: 19 Jan 2017, 14:06

Re: OnExtensionResponse - Not Firing.

Postby meanvel » 18 Apr 2017, 22:41

Oh wait... I thought you were inside a extension, here is a client request of mine... No problems here.

Code: Select all

public class getDataHandler extends BaseClientRequestHandler {
    @Override
    public void handleClientRequest(User Sender, ISFSObject Params) {
        IDBManager dbManager = getParentExtension().getParentZone().getDBManager();
        Connection dbConnection;

        try {
            dbConnection = dbManager.getConnection();

            //Get data from database
            String Query = "SELECT id, user, m, r, c, avatar FROM users WHERE id=" + Params.getInt("dbId");
            ISFSArray Result = dbManager.executeQuery(Query, new Object[]{});

            dbConnection.close();

            //Send data back to player
            send("myData", Result.getSFSObject(0), Sender);
        } catch (SQLException e) {
            trace(ExtensionLogLevel.WARN, "Get mysql data failed for (" + Params.getInt("dbId") + ")dbId.");
        }
       
       
        //Flash
           private function onExtensionResponse(evt:SFSEvent):void {
      var Response:String = evt.params.cmd;
      var Data:ISFSObject = evt.params.params as ISFSObject;
      
      //Handle user data
      if (Response == "myData"){
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: OnExtensionResponse - Not Firing.

Postby Lapo » 19 Apr 2017, 08:48

My suggestion is to try something simpler, without the database in the way.
Add another request handler that takes any parameter and sends a response back to the client, without touching the DB.

See if that works, which is expected :)

Once that's working try reintegrating the database code. By the way is there a reason why you're building the query manually instead of just using the DBManager.executeQuery(...) API? That's easier to use.

Thanks
Lapo
--
gotoAndPlay()
...addicted to flash games
Sanity17
Posts: 4
Joined: 27 Apr 2014, 23:51

Re: OnExtensionResponse - Not Firing.

Postby Sanity17 » 20 Apr 2017, 22:04

Okay took your suggestions on board.

It was the SQL that stopped it for some reason.

Bacxk to drawing board and now have:

Code: Select all

   @Override
   public void handleClientRequest(User user, ISFSObject objIn)
   {
                String Target = objIn.getUtfString("Target");
      IDBManager dbManager = getParentExtension().getParentZone().getDBManager();
      String sql = "SELECT *" + "FROM `pets`" + "WHERE `name` = '" + Target + "'";
      
      try
        {
         // Obtain a resultset
           ISFSArray res = dbManager.executeQuery(sql);
          
           // Populate the response parameters
           ISFSObject objOut = new SFSObject();
           objOut.putSFSArray("Pet", res);
          
           // Send back to requester
           send("Pet", objOut, user);
                System.out.print(objIn);


***Sends successfully back from the server.

On client revamped to:

Code: Select all

      void ExtResponse (BaseEvent e)
   {
      ISFSObject objIn = (SFSObject)e.Params ["params"];
      string cmd = (string)e.Params ["cmd"];



      if (cmd == "Char") {
         Debug.Log ("Name: " + objIn);
         objIn.GetSFSArray ("Pet");
         foreach (SFSArray Pet in objIn.GetSFSArray("Pet")) {
            Debug.Log (Pet);
         }
      }


Now getting: InvalidCastException: Cannot cast from source type to destination type. on trying to retrieve any data, Will keep plugging away at this.
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: OnExtensionResponse - Not Firing.

Postby Lapo » 21 Apr 2017, 07:37

Try dumping the object sent from server side when you receive at the client:

Code: Select all

void ExtResponse (BaseEvent e)
{
      ISFSObject objIn = (SFSObject)e.Params ["params"];
      string cmd = (string)e.Params ["cmd"];

      if (cmd == "Char")
   {
      Debug.Log (objIn.GetDump());
    }
}


This will show the whole structure of the object.

cheers
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 46 guests