Put users into a SFSObject?

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
Carl Lydon
Posts: 298
Joined: 12 Nov 2007, 16:15
Location: NYC
Contact:

Put users into a SFSObject?

Postby Carl Lydon » 01 Nov 2016, 19:12

Hello,

To communicate between room extension I am using "handleInternalMessage" which requires me to package up my parameters and parse them on the other side.

I'm mostly using SFSObjects.

Sometimes I need to pass a reference to a user to another extension, along with other info. Currently I am sending the userId and finding the user on the other side, but I would much rather just send the User object and skip the extra steps. I know you can just pass the User as a generic object and then convert to User, but this doesn't work when passing multiple parameters along with the user.

Is there a way to put a user into a SFSObject?

-Carl
User avatar
Lapo
Site Admin
Posts: 23009
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Put users into a SFSObject?

Postby Lapo » 02 Nov 2016, 09:19

Hi,
a couple of comments.

I would not recommend to use SFSObject to pass data around between Extensions. SFSObject is pretty limited in the types that it supports and it is designed for network transport, not for local object passing.

Since handleInternalMessage requires a generic Object (the root of the type hierarchy) you are severely limiting your options with something like SFSObject.

A much better strategy is to setup your own "protocol" for each command you create for the handleInternalMessage method. Here's an example. Suppose we have these two commands:

1- changeName (takes 2 Strings)
2- storeUser(takes a User)

In command #1 we need two String params, so we can ahead and create and object for this:

Code: Select all

class ChangeNameData
{
    public ChangeNameData(String oldName, String newName);
    {
      ...
    }
}


Now we can use this object to pass the parameters of the "changeName" command, which will look like this:

Code: Select all

   
   @Override
   public Object handleInternalMessage(String cmdName, Object params)
   {
      if (cmdName.equals("changeName"))
      {
         ChangeNameData data = (ChangeNameData) params;
         
         // Rest of the changeName logic here...
      }
      
      else if (cmdName.equals("storeUser"))
      {
         User user = (User) params;
         
         // Rest of the logic here...
      }
   }


Makes sense?
I've also added the implementation of the "storeUser" command, which takes only one parameter.

The whole idea is that you're totally free to plan which object will be used for each call between Extension. The only caveat is to pay attention to converting (casting) each object to the appropriate type.

Hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
Carl Lydon
Posts: 298
Joined: 12 Nov 2007, 16:15
Location: NYC
Contact:

Re: Put users into a SFSObject?

Postby Carl Lydon » 02 Nov 2016, 13:58

Thanks. Your example helps a lot. So, if I want to pass a string along with a User, I just make a Java class that accepts those parameters, create a Java object and pass that.
User avatar
Lapo
Site Admin
Posts: 23009
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Put users into a SFSObject?

Postby Lapo » 02 Nov 2016, 14:37

Yes, that's the essence of all OOP programming. Encapsulating data (and behavior) :)
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: heroumi and 50 guests