Best Practice on Server Side Variable Management

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

Moderators: Lapo, Bax

wiflom
Posts: 1
Joined: 29 May 2011, 13:55

Best Practice on Server Side Variable Management

Postby wiflom » 29 May 2011, 18:03

Hello

I started trying to work with SFS2X. A lot of stuff is really cool and fancy to use. :-)

But I wanted to ask if there is a best practice guideline to manage server side variables?
Is it better to use the UserVariable or the setProperty method of the User? Or even the setProperty method on the session? Or any other possibilities?

Are there any advantages?

For example I want to attach de users ID from the DB as a variable to the user. So that I can recognize him.

Thanks for response.
User avatar
Bax
Site Admin
Posts: 4609
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Postby Bax » 30 May 2011, 08:03

User Variables are meant to be shared among all the clients in a Room. So if this is what you need (for example you want to save the user's nickname and display it on all the clients) than you should use a User Variable.
For other cases (for example the one you mentioned, I think) you should use the user properties, which aren't transmitted to the clients.
Paolo Bax
The SmartFoxServer Team
pauloamorimbr
Posts: 17
Joined: 05 Nov 2010, 15:31

Postby pauloamorimbr » 31 May 2011, 14:28

So...what about server variables?

I mean...if I want to have a map on server memory and control the changes on it, or if I need to control the state of my game...not in a room, but on the whole server. How do I set/read some server/zone variable?
User avatar
Bax
Site Admin
Posts: 4609
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Postby Bax » 01 Jun 2011, 08:44

pauloamorimbr, I don't understand your question. Of course on the server side you can use whatever variables you need to keep the state of your game.
Paolo Bax
The SmartFoxServer Team
pauloamorimbr
Posts: 17
Joined: 05 Nov 2010, 15:31

Postby pauloamorimbr » 01 Jun 2011, 18:07

Well, sorry. Maybe I just missed some basics...but...

Since I have a extension and I only get my code running when it's called by the whole SFS framework, all my logic is isolated on small classes and all my data is persisted on database....and I'm not sure it's the right thing to do.

Lets compare this to...like...JSP or PHP where I have the "application" space to store my data on memory...do I have something like that on SFS?

When my extension inits I'd like to put a string on the memory and I'd like to have that string read on another event later, when, lets say, a user logs in, or sends a message.

Did I miss something?
udikantz
Posts: 153
Joined: 24 Sep 2009, 09:36

Postby udikantz » 02 Jun 2011, 11:22

According to the docs you have 2 ways for handling runtime objects for extension use....

1. not recommended - use singleton classes.... you can put all your desired logic / helpers in a separate jar / project and access the data you need by single ton classes ( you should read on hot to add .jar file to your projects)

2. the recommended way - inside your extension store what ever you want and you can access this data through the handlers supplied by SFS2x.

example:

Code: Select all

public class MyExtension extends com.smartfoxserver.v2.extensions.SFSExtension {
     
    private Object objForRunTimeAccess;

    public void init() {
          objForRunTimeAccess = new Object();
    }

  public Object getObjForRunTimeAccess() {
        return objForRunTimeAccess;
    }
}


later on you could access your custom object from the various handlers supplied by sfs2x, example:

Code: Select all

public class LoginHandler extends BaseServerEventHandler {

    @Override
    public void handleServerEvent(ISFSEvent event) throws SFSException {


        if(getParentExtension() instanceof  MyExtension )
        {
            MyExtension te = (MyExtension )getParentExtension();
            Object myCustomObject = te.getObjForRunTimeAccess();
        }
    }
}
diwip games - co founder & CPO.

http://www.diwip.com
pauloamorimbr
Posts: 17
Joined: 05 Nov 2010, 15:31

Postby pauloamorimbr » 02 Jun 2011, 12:45

OMG!

I feel so dumb right now!
I've been looking for this "getParentExtension()" thing for some months already and never realized I could use it this way! Damn!

Thanks a ton udikantz! :D

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 55 guests