Reactor (Flash Client)

Reactor is Union's full featured ActionScript 3.0 framework for creating connected applications and content for Adobe Flash. With Reactor, you can create chat, whiteboards, real-time multiplayer games, meeting applications, collaborative editing tools, and shared interfaces that run on the Web in Flash Player or on the desktop in Adobe AIR.

Backed by the battle-tested Union Server, Reactor's clean, easy-to-use API makes it almost trivial to build complex multiuser web applications. Like this fully functional chat application, created with only 74 lines of ActionScript, and not a single line of server-side code:

The complete code for the chat is shown below. To get started building your own multiuser Flash applications, follow the Reactor Chat Tutorial.

package {
  import flash.display.Sprite;
  import flash.events.KeyboardEvent;
  import flash.text.TextField;
  import flash.text.TextFieldType;
  import flash.ui.Keyboard;

  import net.user1.reactor.IClient;
  import net.user1.reactor.Reactor;
  import net.user1.reactor.ReactorEvent;
  import net.user1.reactor.Room;

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

    public function UnionChat () {
      // 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("chatRoom");
      chatRoom.addMessageListener("CHAT_MESSAGE", chatMessageListener);
      chatRoom.join();
    }

    // Creates the user interface
    protected function buildUI ():void {
      incomingMessages = new TextField;
      incomingMessages.border = true;
      incomingMessages.background = true;
      incomingMessages.width = 400;
      incomingMessages.height = 200;

      outgoingMessages = new TextField;
      outgoingMessages.type = TextFieldType.INPUT;
      outgoingMessages.border = true;
      outgoingMessages.background = true;
      outgoingMessages.width = 400;
      outgoingMessages.height = 20;
      outgoingMessages.y = 210;
      outgoingMessages.addEventListener(KeyboardEvent.KEY_UP, keyUpListener);
      addChild(incomingMessages);
      addChild(outgoingMessages);
    }

    // Keyboard listener for outgoingMessages
    protected function keyUpListener (e:KeyboardEvent):void {
      if (e.keyCode == Keyboard.ENTER) {
        chatRoom.sendMessage("CHAT_MESSAGE",
                             true,
                             null,
                             outgoingMessages.text);
        outgoingMessages.text = "";
      }
    }

    // Method invoked when a chat message is received
    protected function chatMessageListener (fromClient:IClient,
                                            messageText:String):void {
      incomingMessages.appendText("Guest"
                                  + fromClient.getClientID()
                                  + " says: "
                                  + messageText + "\n");
    }
  }
}