Chat with User Login

This example uses ReactorGUI, Union's user-interface component set, to create a chat application with user registration and login.

The above chat demonstrates how to use the following ReactorGUI features:

  • The AccountBar component, whose buttons trigger login, logoff, and account registration actions
  • The RegisterWindow component, used for user-account registration
  • The LoginWindow component, used for user-account login
  • The ChatPanel component, which supplies the user interface for a chat room

The Code

The code for the above chat follows. Code for the individual user interface components in the chat is available as part of ReactorGUI.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
  xmlns:union="net.user1.reactor.ui.*"
  layout="vertical"
  initialize="initializeListener(event)">

<!--
// =============================================================================
// CSS STYLING
// =============================================================================
-->
<mx:Style>
  Application { backgroundColor: "0xDDDDDD"}
</mx:Style>


<!--
// =============================================================================
// SCRIPTING
// =============================================================================
-->
<mx:Script>
<![CDATA[
  import mx.events.FlexEvent;
 
  import net.user1.reactor.IClient;
  import net.user1.reactor.Reactor;
  import net.user1.reactor.ReactorEvent;
  import net.user1.reactor.Room;
  import net.user1.reactor.RoomManagerEvent;
  import net.user1.reactor.chat.ChatClient;
 
// =============================================================================
// VARIABLES
// =============================================================================
  [Bindable]
  private var reactor:Reactor;
  [Bindable]
  private var room:Room;

// =============================================================================
// FLEX INITIALIZE LISTENER
// =============================================================================

  /**
   * Triggered when the Flex application has been initialized.
   */

  protected function initializeListener (event:FlexEvent):void {
    reactor = new Reactor();
    reactor.addEventListener(ReactorEvent.READY, reactorReadyListener);
    reactor.addEventListener(ReactorEvent.CLOSE, reactorCloseListener);
    reactor.loadConfig("config.xml");
  }

// =============================================================================
// REACTOR READY LISTENER
// =============================================================================

  /**
   * Triggered when Reactor successfully connects to Union server.
   */

  protected function reactorReadyListener (e:ReactorEvent):void {
    // Make every client in this application an instance of ChatClient
    reactor.getClientManager().setDefaultClientClass(ChatClient);
    // Create the chat room on the server
    room = reactor.getRoomManager().createRoom("examples.chat");
    // Set dependent references
    chatPanel.accountBar.accountManager = reactor.getAccountManager();
    chatPanel.userlist.accountManager = reactor.getAccountManager();
    chatPanel.chatIncomingTextArea.messageManager = reactor.getMessageManager();
    // Register for room-removal notification
    reactor.getRoomManager().addEventListener(RoomManagerEvent.ROOM_REMOVED,
                                              roomRemovedListener);
    // Join the chat room
    room.join();
  }

// =============================================================================
// REACTOR CLOSE LISTENER
// =============================================================================

  /**
   * Triggered when Reactor's connection to Union server is terminated.
   */

  protected function reactorCloseListener (e:ReactorEvent):void {
    chatPanel.accountBar.accountManager = null;
    chatPanel.userlist.accountManager = null;
    chatPanel.chatIncomingTextArea.messageManager = null;
    room = null;
  }

// =============================================================================
// ROOM REMOVED LISTENER
// =============================================================================

  /**
   * Triggered when the chat room is removed. In this application, nothing can
   * remove the room, but the room might be removed by an administrator
   * application, such as UnionAdmin.
   */

  protected function roomRemovedListener (e:RoomManagerEvent):void {
    if (e.getRoom() == room) {
      chatPanel.userlist.accountManager = null;
      chatPanel.chatIncomingTextArea.messageManager = null;
      room = null;
    }
  }
 
]]>
</mx:Script>

<!--
// =============================================================================
// MXML USER INTERFACE CONTROLS
// =============================================================================
-->
  <mx:ApplicationControlBar width="100%">
    <mx:Label text="Union Chat"/>
    <mx:Spacer width="100%" />
    <union:ConnectionStatusPane reactor="{reactor}" />
  </mx:ApplicationControlBar>

  <union:ChatPanel id="chatPanel" room="{room}" width="100%" height="100%"/>
</mx:Application>