Unable to create room

Post here your questions about the HTML5 / JavaScript for SFS2X

Moderators: Lapo, Bax

patmckenna
Posts: 7
Joined: 02 Apr 2013, 13:10

Unable to create room

Postby patmckenna » 13 May 2014, 00:24

Hi, I've just gotten started with the JavaScript API and I'm having trouble creating a room in my zone.

I've successfully Connected, Logged in, and Joined existing rooms, but every time I attempt to create a room through the JavaScript API the following warning appears in the server logs:

Code: Select all

WARN  | com.smartfoxserver.v2.controllers.SystemController-1 | v2.controllers.SystemController |     | java.lang.ClassCastException: java.lang.Short cannot be cast to java.lang.Integer
   com.smartfoxserver.v2.entities.data.SFSObject.getInt(SFSObject.java:411)
   com.smartfoxserver.v2.entities.data.SFSObjectLite.getShort(SFSObjectLite.java:46)
   com.smartfoxserver.v2.controllers.system.CreateRoom.preProcess(CreateRoom.java:101)
   com.smartfoxserver.v2.controllers.system.CreateRoom.execute(CreateRoom.java:264)
   com.smartfoxserver.v2.controllers.SystemController.processRequest(SystemController.java:131)
   com.smartfoxserver.bitswarm.controllers.AbstractController.run(AbstractController.java:96)
   java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
   java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
   java.lang.Thread.run(Unknown Source)


I do have a custom extension running on the zone, but I don't see it in the stack trace at all, so I don't see how it would be the problem. However, when I run the exact same client code when connected to the "BasicExamples" zone, it works.

I'm a little baffled by this, so I'm including any other relevant details I can think of:

  • Our actual game client (using the Unity/C# SmartFox client code) can create these same rooms no problem. In fact, my current workaround to this issue is to log in with the game to create the room, then join it from the JavaScript client.
  • The "Maximum number of Rooms" and "Maximum rooms per user" settings are both set high enough (again, logging in using our game client creates rooms no problem).
  • The "Privilege Manager" is not activated, but it does contain "CreateRoom" in the list of denied requests for the "Guest" profile, whereas that is not the case with "BasicExamples".
  • I'm running server version 2.8.2
  • JavaScript API version 1.1.1

If anyone has any ideas or suggestions they would be very much appreciated.

Thanks in advance,
Patrick
User avatar
Bax
Site Admin
Posts: 4330
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: Unable to create room

Postby Bax » 13 May 2014, 07:35

Can you please show the code sending the Room creation request?
Paolo Bax
The SmartFoxServer Team
patmckenna
Posts: 7
Joined: 02 Apr 2013, 13:10

Re: Unable to create room

Postby patmckenna » 13 May 2014, 14:39

Sure thing. Here it is:

Code: Select all

   function createOrJoinRoom(roomName) {
      var room = sfs.getRoomByName(roomName);

      if (typeof(room) !== "undefined" && room !== null) {
         console.log(room);
         var roomToLeave = -1; // don't leave
         var spectator = true;
         sfs.send(new SFS2X.Requests.System.JoinRoomRequest(roomName, null, roomToLeave, spectator));

      } else {
         console.log("creating room " + roomName);
         var settings = new SFS2X.Requests.RoomSettings(roomName);
         var autojoin = true;
         var roomToLeave = -1;
         sfs.send(new SFS2X.Requests.System.CreateRoomRequest(settings, autojoin));//, roomToLeave));
      }
   }


This function gets invoked in response to a SFS2X.SFSEvent.LOGIN event. If more context is required, just let me know.

I also noticed that my JavaScript client gets disconnected with reason "unknown" approximately 60s after I make the CreateRoomRequest. Adding a delay between the login response and attempting to create the room delays the disconnection by the same amount. I also get disconnected after 60s if I *don't* send the create room request. I find this strange because my server isn't configured to time out that quickly:
  • the server's "session maximum idle time" is set to 300
  • the server's "user maximum idle time" is also set to 300
  • my zone's "overridden user maximum idle time" is set to 520

Another bit of strangeness is that the "session removed" & "user disconnection" messages only show up in the server logs ~90s after the ClassCastExceptionWarning (~30s after the disconnection event fires in the JavaScript client)
User avatar
Lapo
Site Admin
Posts: 19646
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Unable to create room

Postby Lapo » 13 May 2014, 18:06

Disconnections over the internet can happen for all sorts of reasons.
The server will not kick any user out unless they have timed out the idle-time value. However if the client sits there doing nothing it may get disconnected, usually when there are network problems.

The fact that you get an "unknown" cause confirms it. I should also add that normally a decent connection shouldn't drop after 60 seconds.

To avoid these problems you can turn on the LagMonitor (see SmartFox.enableLagMonitor) which will measure the current network latency at regular interval. This also serves as a "heartbeat" signal to keep the connection alive. The default heartbeat is every 4 seconds, you may want to slow it down a bit if you just use it as a keep alive signal, 1 req. every 10-30 seconds will do the trick and waste less bandwidth when tons of users are connected.

Cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
patmckenna
Posts: 7
Joined: 02 Apr 2013, 13:10

Re: Unable to create room

Postby patmckenna » 13 May 2014, 19:17

Thanks for the tip on the LagMonitor Lapo! (here is a link to the docs for those who come after: http://docs2x.smartfoxserver.com/api-do ... LagMonitor)

This did solve the unexpected disconnection. However, I don't think it was due to a network issue as the lag monitor is reporting a pretty steady 30ms ping which is pretty good in my books, and more-so since it was so consistent at dropping after exactly 60s. If it was a connectivity problem I'd expect there to be at least a little variance. Perhaps it is something to do with websockets themselves?

Anyway, now it ping-pongs until the 520s idle timeout triggers, at which point I get a proper disconnected reason: "idle".

However, this is all aside of the main question: why can I not create a room via the JavaScript client? The server is still logging same warning as before.

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

Re: Unable to create room

Postby Bax » 14 May 2014, 08:54

why can I not create a room via the JavaScript client? The server is still logging same warning as before

I did the same test using one of the examples that we provide (AdvancedChat): upon login a Room is created and joined automatically.
Everything works as expected, so there must be something else going on.
Also, the exception you get is quite strange, because it is related to the maximum number of users allowed to enter the Room, but you are not even setting it.
Paolo Bax
The SmartFoxServer Team
patmckenna
Posts: 7
Joined: 02 Apr 2013, 13:10

Re: Unable to create room

Postby patmckenna » 14 May 2014, 16:18

Thanks Paolo. Similarly, I augmented AdvancedChat to function using our custom login handler. It can connect, login to our zone, and join existing rooms, but when I attempt to create a room with it I get the same warning logged on the server and no room is created.

I'm guessing this means it is something wrong with my extension, but based on the contents of the warning I'm not sure what it could be.

You mention that it has to do with the maximum number of users per room... where can I find the setting for that? I don't see it in the admin console (I do see it for pre-existing rooms, but I can't find a default setting for new rooms), and I don't believe I set it anywhere within my extension.

I do have a ROOM_ADDED event handler which calls the setMaxRoomVariablesAllowed(10000), setAutoRemoveMode(SFSRoomRemoveMode.WHEN_EMPTY), and setRoomVariables(...) methods (part of our solution to persistent room variables, implemented before that was a built-in feature. :wink: We have similar code to persist user variables as well).

We also have a SFSExtensionFilter (InstantiationMode.SINGLE_INSTANCE) that does some behind-the-scenes db work in response to each ROOM_ADDED event, but it doesn't call any SmartFox API methods and always returns FilterAction.CONTINUE.

I'm going to try disabling each of these, but please let me know if any of them sound like they might be the root cause. My greatest confusion comes from the fact that our C# game client can create rooms and doesn't trigger that warning.

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

Re: Unable to create room

Postby Bax » 14 May 2014, 16:47

I'm not sure what is the role of the Extension in the Room creation process.
The max number of users per Room is set in the SFS2X.Requests.RoomSettings object passed to the CreateRoomRequest.
If you don't set it, the default value is 10 (as mentioned in the API documentation)
Paolo Bax
The SmartFoxServer Team
patmckenna
Posts: 7
Joined: 02 Apr 2013, 13:10

Re: Unable to create room

Postby patmckenna » 14 May 2014, 19:18

I've fixed it! It was a problem with my extension, tho I missed this particular filter when I was looking before. This one's only job is to overwrite the room's max users parameter with a value we look up from the db during any call to CreateRoom. For some reason I used the following code to do it:

Code: Select all

params.putShort(CreateRoom.KEY_MAXUSERS, (short) roomSize);


I have no idea why I was casting to a short, or how this code can possibly work via C# and not JavaScript, but it's been changed to putInt, sans-cast, and all is right with the world.

Thanks for your help. Sorry for dragging you along on this wild goose chase.

Cheers,
Patrick
patmckenna
Posts: 7
Joined: 02 Apr 2013, 13:10

Re: Unable to create room

Postby patmckenna » 14 May 2014, 21:09

Scratch that, I spoke too soon. Applying the change I described above does indeed fix my problem for the JavaScript client, but the C# client is now broken and cannot create rooms. I now remember why I set it to cast it to a short... because I got this message in the server logs when I attempted to use an integer:

Code: Select all

WARN  | com.smartfoxserver.v2.controllers.SystemController-1 | v2.controllers.SystemController |     | java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Short
   com.smartfoxserver.v2.entities.data.SFSObject.getShort(SFSObject.java:476)
   com.smartfoxserver.v2.controllers.system.CreateRoom.preProcess(CreateRoom.java:86)
   com.smartfoxserver.v2.controllers.system.CreateRoom.execute(CreateRoom.java:205)
   com.smartfoxserver.v2.controllers.SystemController.processRequest(SystemController.java:130)
   com.smartfoxserver.bitswarm.controllers.AbstractController.run(AbstractController.java:96)
   java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
   java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
   java.lang.Thread.run(Unknown Source)


Slightly different set of lines and opposite message as my first post: Integer cannot be cast to Short.

Is there any documentation on how to achieve this correctly?

This is the whole filter:

Code: Select all

public class PodCapacityFilter extends SysControllerFilter {

   protected Zone zone;
   
   public PodCapacityFilter(Zone parentZone) {
      this.zone = parentZone;
   }

   @Override
   public FilterAction handleClientRequest(User sender, ISFSObject params) throws SFSException {
      if (sender.getZone() != this.zone) return FilterAction.CONTINUE;
      
      // broken when creating room from the C#/Unity client
      params.putInt(CreateRoom.KEY_MAXUSERS, 31);

      // broken when creating room from the JavaScript client
      //params.putShort(CreateRoom.KEY_MAXUSERS, (short) 31);
      
      return FilterAction.CONTINUE;
   }

}


and it is configured in the Extension like so:

Code: Select all

      // Reset filter chain to clean previous filters when hot reloading
      getParentZone().resetSystemFilterChain();
      
      ISystemFilterChain createRoomFilterChain = new SysControllerFilterChain();
      createRoomFilterChain.addFilter("POD Capacity Filter", new PodCapacityFilter(getParentZone()));
      getParentZone().setFilterChain(SystemRequest.CreateRoom, createRoomFilterChain);
User avatar
Lapo
Site Admin
Posts: 19646
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Unable to create room

Postby Lapo » 15 May 2014, 08:37

Hi,
this is a pretty tricky case, so no wonder you couldn't get it to work. :)

Without too many tech details, Javascript doesn't support types such as Byte, Short, Float etc... it basically only supports Longs and Doubles. Everything else must be "emulated" to map onto the other numeric types supported in Java/C# etc...

Because of this there's some automatic conversion that you don't see, behind the scenes.
Since you are working with both C# and JS clients and you're altering raw data coming from the clients you will need an extra bit of code to handle the two client types.

I think you just need this:

Code: Select all

@Override
public FilterAction handleClientRequest(User sender, ISFSObject params) throws SFSException {
      if (sender.getZone() != this.zone) return FilterAction.CONTINUE;
   
   if (params instanceof SFSObjectLite)
      params.putInt(CreateRoom.KEY_MAXUSERS, 31);

   else
      params.putShort(CreateRoom.KEY_MAXUSERS, 31);
   
   return FilterAction.CONTINUE;
}


Let me know if it works.
Lapo

--

gotoAndPlay()

...addicted to flash games
patmckenna
Posts: 7
Joined: 02 Apr 2013, 13:10

Re: Unable to create room

Postby patmckenna » 15 May 2014, 14:53

Thank you! I was trying to figure out how to split the two cases, but I was not getting it on my own.

Your SFSObjectLite solution does indeed work.

Cheers,
Patrick
User avatar
Lapo
Site Admin
Posts: 19646
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Unable to create room

Postby Lapo » 15 May 2014, 16:54

Glad that it is working :)
Lapo

--

gotoAndPlay()

...addicted to flash games
darkengine
Posts: 3
Joined: 14 Apr 2017, 08:08

Re: Unable to create room

Postby darkengine » 05 Jun 2017, 15:09

Just for update, param.getInt() and param.getShort() also need to detect params' type before using for AS3 client.

Return to “SFS2X HTML5 / JavaScript API”

Who is online

Users browsing this forum: No registered users and 1 guest