Serverside Room Creation

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

Moderators: Lapo, Bax

Post Reply
y_nk
Posts: 5
Joined: 12 Nov 2010, 15:53

Serverside Room Creation

Post by y_nk »

Hi there,

i've been talking about it in a different threads but since the problem looks getting bigger and bigger, i prefer putting all the stuff related in a unique topic.

My need is to create rooms that are owned by the server. To do so, i created an extension that does the tricks.

This is my handleClientRequest method :

Code: Select all

        SmartFoxServer server = SmartFoxServer.getInstance();

        SFSZone zone = (SFSZone) server.getZoneManager().getZoneList().get(1);

        trace("---------- room " + roomname + "request");

        if (zone.getRoomByName(roomname) == null)
        {
            trace("---------- room creation required");

            SFSRoom room = new SFSRoom(roomname);
                room.setMaxUsers(2500);
                room.setActive(true);

            try
            {
                room.addUser(sender);
                sender.addJoinedRoom(room);
            }
            catch (SFSJoinRoomException ex) { trace("error while joining"); }

            HashSet<SFSRoomSettings> flags = new HashSet<SFSRoomSettings>();
                flags.add(SFSRoomSettings.USER_COUNT_CHANGE_EVENT);
                flags.add(SFSRoomSettings.USER_ENTER_EVENT);
                flags.add(SFSRoomSettings.USER_EXIT_EVENT);
                flags.add(SFSRoomSettings.USER_VARIABLES_UPDATE_EVENT);

            room.setFlags(flags);
            room.setAutoRemoveMode(SFSRoomRemoveMode.NEVER_REMOVE);

            try { zone.addRoom(room); }
            catch (SFSTooManyRoomsException ex) { trace("error while adding room"); }
        }

        SFSObject response = new SFSObject();
            response.putUtfString("roomname", roomname);

        send("roomrequest", response, sender);
So, when a user call the "roomrequest" extension request, it creates silently the needed room, and autojoin the user in it.

I also created a "roomleaving" handler which put the user out of the room to have a full custom room join/leave method.

If this looks like working, it does only with one single connexion. If i disconnect my client or if i try to connect an other client, the server throws this :

Code: Select all

15:58:22,008 WARN  [com.smartfoxserver.v2.controllers.SystemController-1] protocol.SFSIoHandler     - 
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Exception: java.lang.NullPointerException
Message: *** Null ***
+--- --- ---+
Stack Trace:
+--- --- ---+
com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.binEncode_UTF_STRING(DefaultSFSDataSerializer.java:1160)
com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.encodeObject(DefaultSFSDataSerializer.java:780)
com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.arr2bin(DefaultSFSDataSerializer.java:544)
com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.array2binary(DefaultSFSDataSerializer.java:530)
com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.encodeObject(DefaultSFSDataSerializer.java:817)
com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.arr2bin(DefaultSFSDataSerializer.java:544)
com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.array2binary(DefaultSFSDataSerializer.java:530)
com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.encodeObject(DefaultSFSDataSerializer.java:817)
com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.obj2bin(DefaultSFSDataSerializer.java:500)
com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.object2binary(DefaultSFSDataSerializer.java:483)
com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.encodeObject(DefaultSFSDataSerializer.java:822)
com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.obj2bin(DefaultSFSDataSerializer.java:500)
com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.object2binary(DefaultSFSDataSerializer.java:483)
com.smartfoxserver.v2.entities.data.SFSObject.toBinary(SFSObject.java:229)
com.smartfoxserver.v2.protocol.binary.BinaryIoHandler.handleWrite(BinaryIoHandler.java:75)
com.smartfoxserver.v2.protocol.SFSIoHandler.onDataWrite(SFSIoHandler.java:325)
com.smartfoxserver.v2.protocol.SFSProtocolCodec.onPacketWrite(SFSProtocolCodec.java:144)
com.smartfoxserver.bitswarm.io.Response.write(Response.java:76)
com.smartfoxserver.v2.api.SFSApi.login(SFSApi.java:457)
com.smartfoxserver.v2.controllers.system.Login.execute(Login.java:125)
com.smartfoxserver.v2.controllers.SystemController.processRequest(SystemController.java:127)
com.smartfoxserver.bitswarm.controllers.AbstractController.run(AbstractController.java:96)
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
java.lang.Thread.run(Thread.java:680)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::



Is what im doing the good way and should i try to solve this bug OR is there an other way to do server owned rooms ?
tpenn
Posts: 95
Joined: 03 Aug 2010, 18:48

Post by tpenn »

I think I can point you in the right direction. I do similar things in my own extension.

Don't instantiate a room directly. Try using one of the versions of getApi().createRoom instead. There's 3 different ones depending on what parameters you need to send it. Be sure you fill out the CreateRoomSettings.

Then, rather than calling room.addUser, again go with the api call getApi().joinRoom.

I expect things will work better for you then. I found that as a rule of thumb, always check if there is an api function to do what you want first.
y_nk
Posts: 5
Joined: 12 Nov 2010, 15:53

Post by y_nk »

Thanks a lot tpenn.

I just checked a SFSApi and it works well. Also, i had to check in a very obscur page of the documentation that you need to pass "null" in the user parameter to make the server owner of the room in api.createRoom() function ; i was looking for smartfoxserver.getUserManager().getServerUser() instead :)

I also found great that using SFSApi link the events again. When i wasn't using this api, the function were working but i hadn't any feedback in the client.

The concept of intern "api" is very rare in flash (i'm a flash dev) ; i didnt know that it could exist, it's the first time i see this.


Anyway... It works well. Thanks. I will let you know when the project will be online :)
Post Reply