Room Extension Variables...

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

Moderators: Lapo, Bax

Devon
Posts: 41
Joined: 12 Apr 2011, 16:18

Room Extension Variables...

Postby Devon » 10 Jan 2013, 13:06

I'm curious, does the room extension get instantiated only once? If so, does this mean I can store data in the extension and have it be there on each request?

Reason being, I'm trying to develop a whiteboard application and I don't want it to send all of the information to everyone every time it changes. Therefore, I don't want to use a RoomVariable. However, I do need to store and send it to new users that enter the room so that they can see what everyone else sees.

I tried adding the following just after the class, before my handleClientRequest function.

Code: Select all

Map<String, ISFSObject> segments = new HashMap<String, ISFSObject>();


Afterwards, I sent an extension request 3 times. The first time, I stored some data in segments and traced it out to verify that it worked. On my 3rd request, I ran the same trace and it was empty.

Also, from within' another class extending BaseClientRequestHandler, how can I call functions located in the RoomExtension?
User avatar
Lapo
Site Admin
Posts: 21276
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Room Extension Variables...

Postby Lapo » 10 Jan 2013, 14:59

The Room Extension is instantiated once and never removed until it's time to dispose the Room.
However this is not to be confused with Client Request Handlers. Those are created anew on each request unless you explicitly choose to avoid it.

See this article: http://docs2x.smartfoxserver.com/Advanc ... extensions
Scroll down a bit until you reach the "Advanced Extension features" section. It will explain how you can make state-ful handlers. By default they are state-less.

Cheers.
Lapo
--
gotoAndPlay()
...addicted to flash games
Devon
Posts: 41
Joined: 12 Apr 2011, 16:18

Re: Room Extension Variables...

Postby Devon » 10 Jan 2013, 19:51

Awesome.

1) Is there a reason, performance wise, as to why they the handlers are instantiated on each request by default?

2) And from a MultiHandler, is there a way to access methods on the RoomExtension class itself?

I was reading the section on "Maintaining state in the Extension" and it suggests to create game-logic in the extension. So I've created my var and created a getter.

Code: Select all

package namespace;

import namespace.requests.CanvasHandler;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.extensions.SFSExtension;
import java.util.HashMap;
import java.util.Map;

public class RoomExtension extends SFSExtension
{
    private Map<String, ISFSObject> segments = new HashMap<String, ISFSObject>();
   
    @Override
    public void init() {
        trace("[INFO] Room Extension Initialized");

        addRequestHandler("c", CanvasHandler.class);
    }
   
    @Override
    public void destroy() {
        super.destroy();
    }
   
    public Map<String, ISFSObject> getSegments() {
        return segments;
    }
}


How can I access my getter method from within CanvasHandler.class?

Thanks!

Edit:
I've figured out #2, would still like an answer on #1. :)
User avatar
Lapo
Site Admin
Posts: 21276
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Room Extension Variables...

Postby Lapo » 11 Jan 2013, 14:52

1) Is there a reason, performance wise, as to why they the handlers are instantiated on each request by default?

It's not much about performance per se, it is about convenience. The SFSExtension parent class can manage your listeners automatically if you let it handle the instantiation. This way you remove of memory leaks due to forgotten listeners, which is the #1 cause of these issues.

2) And from a MultiHandler, is there a way to access methods on the RoomExtension class itself?

Yes in the same way, via the getParentExtension() method

thanks
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: DavinVicy and 18 guests