overview

part 2 of a four-part workshop

revise the chat application we built in part 1

uSimpleChat, Version 2

uSimpleChat v2 will show how to do the following:

  • create a room on the Unity server using ActionScript in Flash
  • join a room on the Unity server using ActionScript in Flash
  • respond to one type of room-join failure
  • clean up after version 1

    we don't want the server to create the chat room, so follow these steps:

  • if Unity is currently running, stop it
  • in your Unity installation directory, edit the file uconfig.xml
  • remove the following xml code from the <INSTANCES> tag of uconfig.xml:
  • <ROOM> <ID>chat</ID> <AUTOJOIN>true</AUTOJOIN> </ROOM>

  • save uconfig.xml
  • start Unity
  • oop development

    Unity development stresses the use of object-oriented programming

    as much as possible, separate code from .fla file

    in uSimpleChat version 2, we change only USimpleChat.as, not uSimpleChat.fla

    creating the chat room with ActionScript

    in v1, udefault.chat room was created on server with uconfig.xml

    in v2, udefault.chat room will be created via actionscript

    each client attempts to create the room; only the first succeeds

    use RoomManager.createRoomOnServer() to create room

    (recall that RoomManager creates, stores, and provides access to Unity namespaces and rooms)

    when to create the chat room

    we want to create the chat room after the client has connected

    so chat-room creation code will go in onClientReady()

    chat room creation code

    add the following room-creation code to onClientReady():

    public function onClientReady ():Void { getRoomManager().createRoomOnServer("chat", "udefault", false, false, 50, null, null); // Display the user interface for this app. getTargetMC().gotoAndStop("simpleChatInterface"); }

    arguments to createRoomOnServer() are:

  • "chat": the room id
  • "udefault": the fully qualified id of the namespace that will contain the room
  • false: the room should not be removed from the server when the last client leaves it
  • false: the server should not automatically broadcast the list of clients in the room
  • 50: the room should allow a maximum of 50 client occupants
  • null: the room has no entry password
  • null: the room has no attributes
  • join the chat room

    the server no longer forces clients to join the room

    hence, we now have to join it manually, with actionscript

    after creating the room, we immediately attempt join it

    Unity processes instructions sequentially, so this approach is fine

    but in future, we'll handle situations where room-joining fails (e.g., room full, wrong password)

    add the room-join code after the creation code:

    public function onClientReady ():Void { getRoomManager().createRoomOnServer("chat", "udefault", false, false, 50, null, null); joinRoom("udefault.chat"); // Display the user interface for this app. getTargetMC().gotoAndStop("simpleChatInterface"); }

    a little debugging information

    to help detect attempts to join non-existent rooms, we add some debugging code

    when a room is not found, the following onJoinNonExistentRoom() method runs automatically:

    public function onJoinNonExistentRoom (e:RoomManagerEvent):Void { var missingRoomID:String = e.getNamespaceID() + "." + e.getRoomID(); trace("Room join failed. Room not found: " + missingRoomID); }

    add the above method to USimpleChat.as, after onClientReady()

    UClient listens to RoomManager events

    hence, USimpleChat can respond to onJoinNonExistentRoom() simply by overriding that method

    the list of methods broadcast by RoomManager can be found in RoomManagerListener

    Unity's event architecture is based on Java's delegation event model

    code complete

    this version of the chat is now done

    export the .swf and try it out!