Page 1 of 1

Best way to create a serverside room Var (owned by server)

Posted: 14 Apr 2012, 06:38
by mixart
I have a server-side room extension and simply want to create a roomvariable for each room.

I have room extensions set and was trying to create the room var on init() like this...


Code: Select all

@Override
   public void init() {
      
      trace("ext running.");
      
      User u = null;
      Room room = getParentRoom();
      ArrayList<RoomVariable> roomVars = new ArrayList<RoomVariable>();
      roomVars.add(new SFSRoomVariable("canvas", ""));
      SmartFoxServer.getInstance().getAPIManager().getSFSApi().setRoomVariables(u, room, roomVars);
      
      //adding draw class
      this.addRequestHandler("draw", DrawHandler.class);


   }


What is this the best way to do this?
I'm getting errors in the console with the above code.

Re: Best way to create a serverside room Var (owned by serve

Posted: 14 Apr 2012, 13:20
by rjgtav
Hello.
What errors are you getting on the console with that code? Please make sure that the room isn't null.

Re: Best way to create a serverside room Var (owned by serve

Posted: 14 Apr 2012, 15:22
by mixart
Here is the error that I see...

Image

If I comment out the setroomvariable line, the error goes away:
//SmartFoxServer.getInstance().getAPIManager().getSFSApi().setRoomVariables(u, room, roomVars);

Re: Best way to create a serverside room Var (owned by serve

Posted: 14 Apr 2012, 15:24
by mixart
Also... there are no users in the room at the time of this error. Since this is all in the init() class, it is calling this when the server starts up.

Re: Best way to create a serverside room Var (owned by serve

Posted: 14 Apr 2012, 20:20
by rjgtav
Hmm... I can't reproduce that error, even when using the same code... Are you using the latest SFS2X Final Release 2.0.1? Can you upload your extension as an attachment (preferably the project)?

[EDIT]
Found it. Please don't create the room when the extension inites (in the init() method). Please create the room only after the server has completely started. You can do something like:

Code: Select all

if(SmartFoxServer.getInstance().isStarted()){
    //create the room
}else{
    //listens to the SERVER_READY event
    this.addEventHandler(SFSEventType.SERVER_READY, ServerReadyHandler.class);
}


Then you need to create a ServerReadyHandler.class which extends the BaseServerEventHandler and which creates the room.

Re: Best way to create a serverside room Var (owned by serve

Posted: 14 Apr 2012, 21:46
by mixart
The room is being created from the config file, but I will try moving it out of the init into another event handler and reply if I have some luck. Appreciate your help.

Re: Best way to create a serverside room Var (owned by serve

Posted: 15 Apr 2012, 06:52
by mixart
Thanks - this seemed to do the trick following your guidance...


Code: Select all

public class SetuproomHandler extends BaseServerEventHandler {

   @Override
   public void handleServerEvent(ISFSEvent evt) throws SFSException {
       User u = null;
       Room room = this.getParentExtension().getParentRoom();
       ArrayList<RoomVariable> roomVars = new ArrayList<RoomVariable>();
       roomVars.add(new SFSRoomVariable("canvas", ""));
       SmartFoxServer.getInstance().getAPIManager().getSFSApi().setRoomVariables(u, room, roomVars);
   }
   
}


These gotcha's kill me! :)