SmartFoxServer 2X Room的加入與建立

Room的型態有二種

  • 靜態Room:Zone Configurator裡設定,當sfs啟動時,Server端會建立靜態Room,適用大廳、固定的聊天室
  • 動態Room:sfs運作期間,可由Server端或Client端建立,適用新開戰場、新開聊天室

sfs裡加入、離開、建立Room等
跟遊戲架構觀念相同
可以很靈活地取得不同範圍的資料
Room還可分為一般遊戲(Room屬性isGame)

  • 一般Room:普通的聊天室、大廳等
  • 遊戲Room:專為遊戲房間設計,會自動指定player id,以容易判斷房間內的user,及允許旁觀者(spectators)進來

 

Client端進入Room請求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var sfs:SmartFox = new SmartFox();
sfs.addEventListener(SFSEvent.ROOM_JOIN, onJoin);
sfs.addEventListener(SFSEvent.ROOM_JOIN_ERROR, onJoinError);
sfs.send( new JoinRoomRequest("The Lobby") );
 
public function onJoin(evt:SFSEvent):void
{
trace("Joined Room: " + evt.params.room.name);
}
 
public function onJoinError(evt:SFSEvent):void
{
trace("Join failed: " + evt.params.errorMessage);
}

Server端指定user進入Room

1
2
3
4
5
6
7
User user = (User) event.getParameter(SFSEventParam.USER);
Room Lobby = getParentExtension().getParentZone().getRoomByName("The Lobby");
 
if (Lobby == null)
throw new SFSException("沒有 Lobby 房間");
 
getApi().joinRoom(user, Lobby);

Client端建立Room

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
smartFox.addEventListener(SFSEvent.ROOM_ADD, onRoomAdded)
smartFox.addEventListener(SFSEvent.ROOM_CREATION_ERROR, onRoomCreationError)
 
// Create a new Chat Room
var settings:RoomSettings = new RoomSettings("Piggy's Chat Room")
settings.maxUsers = 40
settings.groupId = "ChatGroup"
 
smartFox.send(new CreateRoomRequest(settings))
 
function onRoomAdded(evt:SFSEvent):void
{
trace("A new Room was added: " + evt.params.room )
}
 
function onRoomCreationError(evt:SFSEvent):void
{
trace("An error occurred while attempting to create the Room: " + evt.params.errorMessage)
}

參考資料:
SFS2X Docs / DevelopmentBasics / join-and-create-rooms

posted @ 2013-02-01 16:55  星尘  阅读(674)  评论(0编辑  收藏  举报