User Account Registration/Login
This example demonstrates a simple Union user-account management panel.
Note: this example focuses on the Reactor API, and therefore shows only the bare minimum code required to create an account-management user interface.
The user-account example above demonstrates the following Reactor techniques:
- Creating a user account
- Removing a user account
- Logging into a user account
- Logging off of a user account
- Setting a persistent client attribute that is saved to the server
- Automatically reconnecting to Union Server after a disconnection
The Code
Here's the code for the user-account management panel:
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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | package { import flash.display.Sprite; import flash.events.MouseEvent; import flash.text.TextField; import flash.text.TextFieldType; import flash.text.TextFormat; import flash.text.TextFormatAlign; import net.user1.reactor.AccountEvent; import net.user1.reactor.AccountManager; import net.user1.reactor.AccountManagerEvent; import net.user1.reactor.AttributeEvent; import net.user1.reactor.ClientEvent; import net.user1.reactor.IClient; import net.user1.reactor.Reactor; import net.user1.reactor.ReactorEvent; import net.user1.reactor.Status; import net.user1.reactor.UserAccount; public class UnionUserLogin extends Sprite { //============================================================================== // VARIABLES //============================================================================== protected var useridInput:TextField; protected var passwordInput:TextField; protected var createAccountButton:Sprite; protected var removeAccountButton:Sprite; protected var loginButton:Sprite; protected var logoffButton:Sprite; protected var setAttributeButton:Sprite; protected var output:TextField; protected var reactor:Reactor; protected var accountManager:AccountManager; //============================================================================== // CONSTRUCTOR //============================================================================== public function UnionUserLogin () { // Create user interface createUI(); // Create Reactor reactor = new Reactor(); reactor.getLog().setLevel("DEBUG"); reactor.getConnectionMonitor().setAutoReconnectFrequency(10000); reactor.addEventListener(ReactorEvent.READY, readyListener); reactor.addEventListener(ReactorEvent.CLOSE, closeListener); // Register account manager listeners accountManager = reactor.getAccountManager(); accountManager.addEventListener(AccountManagerEvent.CREATE_ACCOUNT_RESULT, createAccountResultListener); accountManager.addEventListener(AccountManagerEvent.REMOVE_ACCOUNT_RESULT, removeAccountResultListener); accountManager.addEventListener(AccountEvent.LOGIN_RESULT, loginResultListener); accountManager.addEventListener(AccountEvent.LOGIN, loginListener); accountManager.addEventListener(AccountEvent.LOGOFF_RESULT, logoffResultListener); accountManager.addEventListener(AccountEvent.LOGOFF, logoffListener); // Connect to Union reactor.connect("tryunion.com", 80); } //============================================================================== // REACTOR LISTENERS //============================================================================== protected function readyListener (e:ReactorEvent):void { out("Connected to Union."); } protected function closeListener (e:ReactorEvent):void { out("Disconnected."); } //============================================================================== // ACCOUNT MANAGER EVENT LISTENERS //============================================================================== protected function createAccountResultListener (e:AccountManagerEvent):void { out("Create account result for [" + e.getUserID() + "]: " + e.getStatus()); } protected function removeAccountResultListener (e:AccountManagerEvent):void { out("Remove account result for [" + e.getUserID() + "]: " + e.getStatus()); } //============================================================================== // ACCOUNT EVENT LISTENERS //============================================================================== protected function loginResultListener (e:AccountEvent):void { out("Login result for [" + e.getUserID() + "]: " + e.getStatus()); } protected function loginListener (e:AccountEvent):void { var hairColour:String; // Display a login message out("Account logged in [" + e.getUserID() + "]"); // Register for attribute assignment result e.getAccount().addEventListener(AttributeEvent.SET_RESULT, setAttributeResultListener); // Retrieve the value of the hairColour attribute hairColour = e.getAccount().getAttribute("hairColour"); if (hairColour != null) { // Display the user's saved hair colour out("Loaded user attribute hairColour. Value: " + hairColour); } else { // Ask the user to saved a hair colour out("User attribute hairColour is not yet in the database."); out("To save your hair colour, click 'Set Hair Colour'."); out("Next time you login, your hair colour will be loaded automatically."); } } protected function logoffResultListener (e:AccountEvent):void { out("Logoff result for [" + e.getUserID() + "]: " + e.getStatus()); } protected function logoffListener (e:AccountEvent):void { out("Account logged off [" + e.getUserID() + "]"); // Unregister for attribute assignment result e.getAccount().removeEventListener(AttributeEvent.SET_RESULT, setAttributeResultListener); } //============================================================================== // CLIENT EVENT LISTENERS //============================================================================== protected function setAttributeResultListener (e:AttributeEvent):void { if (e.getChangedAttr().name == "hairColour") { out("Set hair colour result: " + e.getStatus()); } } //============================================================================== // UI CONTROL //============================================================================== public function out (message:String):void { output.appendText(message + "\n"); output.scrollV = output.maxScrollV; } //============================================================================== // UI LISTENERS //============================================================================== protected function createAccountClickListener (e:MouseEvent):void { accountManager.createAccount(useridInput.text, passwordInput.text); } protected function removeAccountClickListener (e:MouseEvent):void { accountManager.removeAccount(useridInput.text, passwordInput.text); } protected function loginClickListener (e:MouseEvent):void { accountManager.login(useridInput.text, passwordInput.text); } protected function logoffClickListener (e:MouseEvent):void { accountManager.logoff(useridInput.text, passwordInput.text); } protected function setAttributeClickListener (e:MouseEvent):void { var selfAccount:UserAccount = accountManager.selfAccount(); if (selfAccount && selfAccount.isLoggedIn()) { selfAccount.setAttribute("hairColour", "green"); } else { out("Please login before setting the user's hair colour."); } } //============================================================================== // UI CREATION //============================================================================== protected function createUI ():void { useridInput = createInputField("ENTER USERID HERE"); passwordInput = createInputField("ENTER PASSWORD HERE"); createAccountButton = createButton("Create Account", createAccountClickListener); removeAccountButton = createButton("Remove Account", removeAccountClickListener); loginButton = createButton("Login", loginClickListener); logoffButton = createButton("Logoff", logoffClickListener); setAttributeButton = createButton("Set Hair Colour", setAttributeClickListener); output = createOutputField(); useridInput.y = 10; passwordInput.y = 35; createAccountButton.x = 50; createAccountButton.y = 60; removeAccountButton.x = 170; removeAccountButton.y = 60; loginButton.x = 50; loginButton.y = 90; logoffButton.x = 170; logoffButton.y = 90; setAttributeButton.x = 105; setAttributeButton.y = 120; output.y = 150; addChild(useridInput); addChild(passwordInput); addChild(createAccountButton); addChild(removeAccountButton); addChild(loginButton); addChild(logoffButton); addChild(setAttributeButton); addChild(output); } protected function createInputField (text:String):TextField { var t:TextField = new TextField(); t.text = text; t.type = TextFieldType.INPUT; t.x = 10; t.width = 350; t.height = 20; t.background = true; t.border = true; return t; } protected function createOutputField ():TextField { var t:TextField = new TextField(); t.x = 10; t.width = 350; t.height = 165; t.background = true; t.border = true; return t; } protected function createButton (label:String, clickListener:Function):Sprite { var t:TextField = new TextField(); t.text = label; t.x = 10; t.width = 110; t.height = 20; t.background = true; t.border = true; t.selectable = false; var buttonFormat:TextFormat = new TextFormat(); buttonFormat.bold = true; buttonFormat.align = TextFormatAlign.CENTER; buttonFormat.font = "_sans"; t.setTextFormat(buttonFormat); var s:Sprite = new Sprite; s.buttonMode = true; s.mouseChildren = false; s.addEventListener(MouseEvent.CLICK, clickListener); s.addChild(t); return s; } } } |