Create Room on Extension init

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

Moderators: Lapo, Bax

wallis2xk
Posts: 43
Joined: 01 Sep 2006, 10:00
Location: United Kingdom

Create Room on Extension init

Postby wallis2xk » 03 Mar 2011, 18:13

I'm trying to create a game room on the init function of my Zone extension

Code: Select all

try {
            CreateRoomSettings crs = new CreateRoomSettings();
            crs.setName("Test Room");
            crs.setMaxUsers(2);
            crs.setMaxVariablesAllowed(0);
            crs.setGame(true);
            crs.setMaxSpectators(0);
            getApi().createRoom(getParentZone(), crs, null);
        } catch (SFSCreateRoomException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }


but upon starting the server receive the error on the createRoom line

Code: Select all

Exception: java.lang.NullPointerException
Message: *** Null ***
Description: Extension initialization failed
+--- --- ---+
Stack Trace:
+--- --- ---+
com.smartfoxserver.bitswarm.io.Response.write(Response.java:74)
com.smartfoxserver.v2.api.response.SFSResponseApi.notifyRoomAdded(SFSResponseApi.java:110)
com.smartfoxserver.v2.api.SFSApi.createRoom(SFSApi.java:622)
com.smartfoxserver.v2.api.SFSApi.createRoom(SFSApi.java:578)


Am I doing something wrong or is this not possible?

Thanks
User avatar
Bax
Site Admin
Posts: 4519
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Postby Bax » 04 Mar 2011, 10:01

We will look into this, but... what is the point in creating a room during Extension initialization? Simply create a static room in the Admin Tool.

Anyway, as a workaround you can use a different signature of the getApi().createRoom() method, avoiding events to be fired.
Paolo Bax
The SmartFoxServer Team
wallis2xk
Posts: 43
Joined: 01 Sep 2006, 10:00
Location: United Kingdom

Postby wallis2xk » 04 Mar 2011, 10:11

I'm doing it this way as there are going to be 100+ game rooms and creating them in the Admin would be quite tedious, especially if a setting needed changing :)

I'll give the workaround a go, thanks
User avatar
Lapo
Site Admin
Posts: 21603
Joined: 21 Mar 2005, 09:50
Location: Italy

Postby Lapo » 05 Mar 2011, 09:55

During the init(), at server boot, the network engine is not started yet but you are using the version of the createRoom() method that sends an update to everyone. This causes the problem. The server is not ready to communicate to anyone.

You just need to use the longer overloaded version of createRoom() where you can specify that you don't want to update the clients.

See the javadoc for further details.
Lapo
--
gotoAndPlay()
...addicted to flash games
Luc
Posts: 18
Joined: 17 Jun 2007, 21:05

Postby Luc » 06 Sep 2011, 15:02

I am in the same situation of having to create a large number of rooms and NPCs when the extension starts.

The solution I have gone for is to add a listener for SFSEventType.SERVER_READY and create the rooms and NPCs in there, as suggested in the javadoc: http://docs2x.smartfoxserver.com/api-do ... boolean%29

The downside is that this event doesn't trigger when the extension gets reloaded, so now I need to restart the server every time to have my rooms and NPCs created. Is there a better way?
jamalsoueidan
Posts: 153
Joined: 15 Aug 2011, 16:33

Postby jamalsoueidan » 06 Sep 2011, 21:33

Just create a small app that connect to socket server and create those rooms with all those settings you have :)

No need to make extension for it :)

Just some admin client API for yourself to initialize your server.
---------------------------------------------------
Jamal Soueidan
http://linkedin.com/in/jamalsoueidan
User avatar
marsoups
Posts: 166
Joined: 14 Apr 2008, 03:30

Re:

Postby marsoups » 18 Sep 2012, 01:02

Lapo wrote:During the init(), at server boot, the network engine is not started yet but you are using the version of the createRoom() method that sends an update to everyone. This causes the problem. The server is not ready to communicate to anyone.

You just need to use the longer overloaded version of createRoom() where you can specify that you don't want to update the clients.

See the javadoc for further details.


I think this is an important feature to be able to handle dynamic creation of room at load time, it's a feature that developers should have at their disposal and crazy workarounds...
Last edited by marsoups on 18 Sep 2012, 01:18, edited 1 time in total.
User avatar
marsoups
Posts: 166
Joined: 14 Apr 2008, 03:30

Re: Create Room on Extension init

Postby marsoups » 18 Sep 2012, 01:06

Nevermind, I found what is meaned; if you use the "joinRoom" with these params

fireClientEvent - fire a client side Event
fireServerEvent - fire a server side Event


both set to false it won't fail. Perhaps this could be made clear in the documentation for the minimal request.
User avatar
Lapo
Site Admin
Posts: 21603
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Create Room on Extension init

Postby Lapo » 18 Sep 2012, 10:06

This is actually documented in the init() method of the Extensions:

Please note that when the Extension is initialized the server is still running the boot sequence and it's not yet ready to communicate with any clients. At this stage any API call that involves communication with clients will fail and should be avoided. You should instead add a listener for the SERVER_READY event and move your API calls there.


So if you want to create a Room you should turn off the fireClientUpdate flag, or register for the SERVER_READY event and start those Rooms there.
Lapo

--

gotoAndPlay()

...addicted to flash games
Tatanan
Posts: 112
Joined: 07 Jan 2014, 12:12
Contact:

Re: Create Room on Extension init

Postby Tatanan » 09 Jan 2014, 12:11

Hello Lapo.
Are you suggesting then to use
createRoom(Zone zone, CreateRoomSettings params, User owner, boolean joinIt, Room roomToLeave, boolean fireClientEvent, boolean fireServerEvent)
from class SFSApi?

I have employed getParentZone().createRoom(settings), from SFSZone class, which works but I think this is not the recommended way to do it, right?

In general, is it totally recommended to use setters methods from entities classes? I mean, knowing that they are not notifying users, aren't they equivalent?

Thank you.
Last edited by Tatanan on 13 Jan 2014, 10:23, edited 1 time in total.
User avatar
Lapo
Site Admin
Posts: 21603
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Create Room on Extension init

Postby Lapo » 09 Jan 2014, 14:16

No, in general it is recommended to alway use the SFSApi class, which centralizes most of the server side API.
If you call the other server objects directly you will miss the event dispatching and more.

There's also three more specialized API objects:
SFSGameApi
SFSBuddyApi
SFSMMOApi

They are all accessible from:

Code: Select all

SmartFoxServer.getInstance().getApiManager()

You can find more details in the "Advanced Topics" section of our doc website:
http://docs2x.smartfoxserver.com/

hope it helps
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: Aldenea, DavinVicy, DerikFent, JerodSa, Kevengar and 28 guests