newFromObject()

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

Moderators: Lapo, Bax

Moonshine|ashish

newFromObject()

Postby Moonshine|ashish » 13 May 2019, 09:59

I am new to smartfox and using Java as Sever API and HTML5 client

Having problem in getting POJO classes data on my javascript client .

In my clientOutGoingData i have converted MY POJO int SFSObject like this

Code: Select all

loginData.clientOutGoingData.putSFSObject("userProperties", SFSObject.newFromObject(new UserProperties()));


I have check for Dump i got the value as array

Code: Select all

"
   (sfs_array) $F:
      (sfs_object) 0:
         (utf_string) V: user property Gameid
         (utf_string) N: userGameId
   (sfs_object) 1:
         (int) V: 10000
         (utf_string) N: tableId
         (sfs_object) 2:
         (utf_string) V: hello POJO
         (utf_string) N: name
      (utf_string) $C: com.sfs.UserProperties
   
"


And trying to fetch it in JavaScript but not able to fetch it like this

Code: Select all

event.data.getSFSObject('userProperties').getUtfString('userGameId');

userGameId as some private varaible. but value is null

Thanks in advance
User avatar
Lapo
Site Admin
Posts: 23009
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: newFromObject()

Postby Lapo » 13 May 2019, 15:25

Hi,
sorry, Javascript clients don't support class serialization because of the inherent differences between strong/static typed languages such as Java and JS.

In this case it would be easier add a toSFSObject() and fromSFSObject() methods to your Java and JS classes to serialize and de-serialize the data that you need to transport from on side to the other.

Example:

Code: Select all

public class LaserGun
{
   private String id;

   private int durabilty;
   private double damagePerSecond;
   private double accuracy;
   
   public LaserGun()
   {
   
   }
   
   
   // ...
   // getters and setters
   // ...
   // ...
   
   public ISFSObject toSFSObject()
   {
      ISFSObject sfso = new SFSObject();
      sfso.putInt("d", durabilty);
      sfso.putDouble("dps", damagePerSecond);
      sfso.putDouble("a", accuracy);
      
      return sfso;
   }
   
   public static LaserGun fromSFSObject(ISFSObject sfso)
   {
      LaserGun gun = new LaserGun();
      
      gun.setDurability( sfso.getInt("d") );
      gun.setDamagePerSecond( sfso.getDouble("dps") );
      gun.setAccuracy( sfso.getDouble("a") );      
      
      return gun;
   }
}


The example requires an empty constructor for the deserialization. You can add more constructors if you want other versions with more parameters, just keep an empty one for the fromSFSObject() method.

Hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
Moonshine|ashish

Re: newFromObject()

Postby Moonshine|ashish » 15 May 2019, 11:04

Thanks lapo.. :)
User avatar
Lapo
Site Admin
Posts: 23009
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: newFromObject()

Postby Lapo » 15 May 2019, 12:45

We've added an article on this topic just yesterday:
https://smartfoxserver.com/blog/object- ... languages/
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 31 guests