About Room and Creation in SmartFoxServer2X With Code Snippets


Room:- One of the fundamental building blocks in the SFS2X framework is the Room object. Rooms allow arranging players so that they can "see" each other and interact together.

Rooms are created in two different ways.

Statically

  • Using the Zone Configurator module in the AdminTool
  • This manner is a useful way to create persistent Rooms in your Zone (e.g. a Lobby) that are initialized as soon as the SmartFoxServer instance is launched.
    OR
  • "In Mannual Manner".we can create the rooms by entering the details manually by opening the zone.xml files existed in zones folder (SFS2X->config->zones).
  • Write logging instructions similar to log4j, save it and restart the server to take effect.

Dynamically

  • Rooms can be created and destroyed at runtime from either the client or the server.
  • There is no difference between Rooms created from one or the other side, however on the server-side it is possible to fine tune certain aspects of Rooms that the client cannot access for security reasons.
  • Simple code to create room from Server side:
  • Simple code to create room at Client side:

> How to Join Room using client API

Joining a Room from the client API requires one line of code (sfs is the SmartFox class instance):

1 sfs.send( new JoinRoomRequest( "The Lobby" ) );

The server will in turn respond with one of the following events:

  1. SFSEvent.ROOM_JOIN, if the operation is successful
  2. SFSEvent.ROOM_JOIN_ERROR, if an error occurred while join room
  3. We need to register the events in our main SmartFox class instance to be notified of the result of the join operation
  4. Basic Client Side code for the above things (I am Using Smartfox client Java api)
01 //Connection and Registration of Events
02 SmartFox sfs= new SmartFox();
03 sfs.addEventListener(SFSEvent.ROOM_JOIN, this );
04 sfs.addEventListener(SFSEvent.ROOM_JOIN_ERROR, this );
05  
06 //JoinRoom Request
07 sfs.send( new JoinRoomRequest( "RoomName" );
08  
09 //handling the server reponses
10 public void dispatch(BaseEvent event) throws SFSException {
11          switch (event.getType()){
12              case SFSEvent.ROOM_JOIN:
13              System.out.println( "JoinRoom Success" );
14              break ;
15              case SFSEvent.ROOM_JOIN_ERROR:
16              System.out.println( "JoinRoom Failed" );
17              break ;
18          }
19 }