flash on 2011-2-9

by Nackybear
Union Platform Jikken 
I want to know how to get Client ID.
クライアントIDの取得の仕方がわからない。。。。。
♥0 | Line 163 | Modified 2011-02-12 13:26:10 | MIT License
play

ActionScript3 source code

/**
 * Copyright Nackybear ( http://wonderfl.net/user/Nackybear )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/5dge
 */

//Union Platform Jikken 
//I want to know how to get Client ID.
//クライアントIDの取得の仕方がわからない。。。。。


package {
  import flash.display.Shape;
  import flash.display.Sprite;
  import flash.events.KeyboardEvent;
  import flash.text.TextField;
  import flash.text.TextFieldType;
  import flash.ui.Keyboard;
  
  import net.user1.reactor.Attribute;
  import net.user1.reactor.IClient;
  import net.user1.reactor.Reactor;
  import net.user1.reactor.ReactorEvent;
  import net.user1.reactor.Room;
  import net.user1.reactor.RoomEvent;
  import net.user1.reactor.SynchronizationState;

  public class UnionChatPart2 extends Sprite {
    // Union objects
    protected var reactor:Reactor;
    protected var chatRoom:Room;
    // User interface objects
    protected var incomingMessages:TextField;
    protected var outgoingMessages:TextField;
    protected var userlist:TextField;
    protected var nameInput:TextField;

    // Constructor    
    public function UnionChatPart2 () {
      // Create the user interface
      buildUI();
      // Make the Reactor object
      reactor = new Reactor();
      // Run readyListener() when the connection is ready
      reactor.addEventListener(ReactorEvent.READY, 
                               readyListener);
      // Connect to the server
      reactor.connect("tryunion.com", 80);
    }
    
    // Method invoked when the connection is ready
    protected function readyListener (e:ReactorEvent):void {
      incomingMessages.appendText("Connected to Union\n");
      chatRoom = reactor.getRoomManager().createRoom(
                                                   "chatRoomA");
      chatRoom.addMessageListener("CHAT_MESSAGE", 
                                  chatMessageListener);
      chatRoom.addEventListener(RoomEvent.JOIN,
                                joinRoomListener);
      chatRoom.addEventListener(RoomEvent.ADD_OCCUPANT,
                                addClientListener);
      chatRoom.addEventListener(RoomEvent.REMOVE_OCCUPANT,
                                removeClientListener);
      chatRoom.addEventListener(RoomEvent.UPDATE_CLIENT_ATTRIBUTE,
                                updateClientAttributeListener);
      chatRoom.join();
    }
    
    // Create the user interface
    protected function buildUI ():void {
      incomingMessages = new TextField;
      incomingMessages.border = true;
      incomingMessages.background = true;
      incomingMessages.width = 300;
      incomingMessages.height = 100;
      incomingMessages.y = 290;

      outgoingMessages = new TextField;
      outgoingMessages.type = TextFieldType.INPUT;
      outgoingMessages.border = true;
      outgoingMessages.background = true;
      outgoingMessages.width = 399;
      outgoingMessages.height = 20;
      outgoingMessages.y = 400;
      outgoingMessages.addEventListener(KeyboardEvent.KEY_UP, 
                                        outgoingKeyUpListener);
                                        
      userlist = new TextField();
      userlist.border = true;
      userlist.background = true;
      userlist.width = 89;
      userlist.height = 100;
      userlist.x = 310;
      userlist.y = 290;
                                        
      nameInput = new TextField();
      nameInput.type = TextFieldType.INPUT;
      nameInput.border = true;
      nameInput.background = true;
      nameInput.width = 399;
      nameInput.height = 20;
      nameInput.y = 430;
      nameInput.addEventListener(KeyboardEvent.KEY_UP, 
                                 nameKeyUpListener);
      
      addChild(incomingMessages);
      addChild(outgoingMessages);
      addChild(userlist);
      addChild(nameInput);
    }
    
    

    
    // Keyboard listener for outgoingMessages
    protected function outgoingKeyUpListener (e:KeyboardEvent):void {
      if (e.keyCode == Keyboard.ENTER) {
          var circle:Shape = new Shape;
          circle.graphics.beginFill(0x636363,1);
            circle.graphics.drawCircle(50,50,20);
            circle.graphics.endFill();
                 stage.addChild(circle);
                 
                 
                  chatRoom.sendMessage("CHAT_MESSAGE",                   
                             true, 
                             null, 
                             outgoingMessages.text);
        outgoingMessages.text = "";
      }
    }
    
    // Keyboard listener for nameInput
    protected function nameKeyUpListener (e:KeyboardEvent):void {
      var self:IClient;
      if (e.keyCode == Keyboard.ENTER) {
          
          
        self = reactor.self();
        self.setAttribute("username", nameInput.text);
        nameInput.text = "";
      }
    }
    
    // Method invoked when a chat message is received
    protected function chatMessageListener (fromClient:IClient, 
                                            messageText:String
                                            ):void {
      incomingMessages.appendText(getUserName(fromClient)
                                  + " says: " 
                                  + messageText + "\n");
      incomingMessages.scrollV = incomingMessages.maxScrollV;
    }
    
    // Method invoked when the current client joins the room
    protected function joinRoomListener (e:RoomEvent):void {
      updateUserList();
    }
    
    // Method invoked when a client joins the room
    protected function addClientListener (e:RoomEvent):void {
      if (e.getClient().isSelf()) {
        incomingMessages.appendText("You joined the chat.\n");
      } else {
        if (chatRoom.getSyncState() != SynchronizationState.SYNCHRONIZING) {
          // Show a "guest joined" message only when the room isn't performing
          // its initial occupant-list synchronization.
          incomingMessages.appendText(getUserName(e.getClient())
                                      + " joined the chat.\n");
        }
      }
      incomingMessages.scrollV = incomingMessages.maxScrollV;
      updateUserList();
    }
    
    // Method invoked when a client leave the room
    protected function removeClientListener (e:RoomEvent):void {
      incomingMessages.appendText(getUserName(e.getClient())
                                  + " left the chat.\n");
      incomingMessages.scrollV = incomingMessages.maxScrollV;
      updateUserList();
    }
    
    // Method invoked when any client in the room 
    // changes the value of a shared attribute
    protected function updateClientAttributeListener (e:RoomEvent):void {
      var changedAttr:Attribute = e.getChangedAttr();
      if (changedAttr.name == "username") {
        if (changedAttr.oldValue == null) {
          incomingMessages.appendText("Guest" + e.getClientID());
        } else {
          incomingMessages.appendText(changedAttr.oldValue);
        }
        incomingMessages.appendText(" 's name changed to "
                                    + getUserName(e.getClient())
                                    + ".\n");
        incomingMessages.scrollV = incomingMessages.maxScrollV;
        updateUserList();
      }
    }
    
    // Helper method to display the room's 
    // clients in the user list
    protected function updateUserList ():void {
      userlist.text = "";
      for each (var client:IClient in chatRoom.getOccupants()) {
        userlist.appendText(getUserName(client) + "\n");
      }
    }
    
    // Helper method to retrieve a client's user name.
    // If no user name is set for the specified client, 
    // returns "Guestn" (where 'n' is the client's id).  
    protected function getUserName (client:IClient):String {
      var username:String = client.getAttribute("username");
        if (username == null) {
          return "Guest" + client.getClientID();
       
          
        } else {
          return username;
        }
    }
  }
}