<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Orbiter Save User Data</title> <!--CSS--> <style type="text/css"> #outPane { border: inset 2px; height: 100px; width: 400px; overflow: auto; padding: 5px; margin-bottom: 5px } </style> <!--Load the Orbiter JavaScript library (non-minified version). Use during development.--> <script type="text/javascript" src="http://unionplatform.com/cdn/Orbiter_latest.js"></script> <!--Load the Orbiter JavaScript library (minified version). Use for production.--> <!--<script type="text/javascript" src="http://unionplatform.com/cdn/Orbiter_latest_min.js"></script>--> <!--Application code--> <script type="text/javascript"> //============================================================================== // VARIABLES //============================================================================== var orbiter; //============================================================================== // INITIALIZATION //============================================================================== function init () { // Create the Orbiter instance, used to connect to and communicate with Union, // then enable automatic reconnection (one attempt every 15 seconds) orbiter = new net.user1.orbiter.Orbiter(); orbiter.getConnectionMonitor().setAutoReconnectFrequency(15000); orbiter.getLog().setLevel(net.user1.logger.Logger.DEBUG); // Register for Orbiter's connection events orbiter.addEventListener(net.user1.orbiter.OrbiterEvent.READY, readyListener, this); orbiter.addEventListener(net.user1.orbiter.OrbiterEvent.CLOSE, closeListener, this); displayMessage("Connecting to Union..."); // Connect to Union Server orbiter.connect("tryunion.com", 80); } //============================================================================== // ORBITER EVENT LISTENERS //============================================================================== // Triggered when the connection is established and // Orbiter is ready to communicate with Union Server function readyListener (e) { displayMessage("Connected."); orbiter.getAccountManager().addEventListener( net.user1.orbiter.AccountManagerEvent.CREATE_ACCOUNT_RESULT, createAccountResultListener, this); orbiter.getAccountManager().addEventListener(net.user1.orbiter.AccountEvent.LOGIN_RESULT, loginResultListener, this); orbiter.getAccountManager().addEventListener(net.user1.orbiter.AccountEvent.LOGOFF, logoffListener, this); } // Triggered when the connection is closed function closeListener (e) { } //============================================================================== // ACCOUNT EVENT LISTENERS //============================================================================== // Triggered when the result of an account creation attempt is received function createAccountResultListener (e) { displayMessage("Create account result for [" + e.getUserID() + "]: " + e.getStatus()); } // Triggered when the result of a login attempt is received function loginResultListener (e) { displayMessage("Login result for [" + e.getUserID() + "]: " + e.getStatus()); // When the current client logs in... if (e.getAccount().isSelf()) { var age = e.getAccount().getAttribute("age"); // Report the value of the permanent attribute "age". if (age == null) { displayMessage("Attribute [age] has not yet been set for this account."); } else { displayMessage("Attribute [age] found. Current value: " + e.getAccount().getAttribute("age")); } // ...save a permanent attribute named "age" in the user's account displayMessage("Saving [41] as the value of the attribute [age].") e.getAccount().setAttribute("age", "41"); } } // Triggered when the result of a login attempt is received function logoffListener (e) { displayMessage("Logged off."); // When an account logs off, Union Server disconnects the corresponding // client. When this client disconnects, it is configured to automatically // reconnect (via the application's config.xml file). So tell the user that // a reconnection is in progress. displayMessage("Reconnecting..."); } //============================================================================== // UI //============================================================================== // Displays a single message on screen function displayMessage (message) { // Make the new message element var msg = document.createElement("span"); msg.appendChild(document.createTextNode(message)); msg.appendChild(document.createElement("br")); // Append the new message to the output pane var outPane = document.getElementById("outPane"); outPane.appendChild(msg); // Trim the chat to 500 messages if (outPane.childNodes.length > 500) { outPane.removeChild(outPane.firstChild); } outPane.scrollTop = outPane.scrollHeight; } function createAccount () { var userid = document.getElementById("userid"); var password = document.getElementById("password"); if (userid.value.length > 0) { orbiter.getAccountManager().createAccount(userid.value, password.value) } } function login () { if (orbiter.self().getAccount() != null) { displayMessage("Already logged in. Please logoff before attempting to login.") return; } var userid = document.getElementById("userid"); var password = document.getElementById("password"); if (userid.value.length > 0) { orbiter.getAccountManager().login(userid.value, password.value) } } function logoff () { var userid = document.getElementById("userid"); var password = document.getElementById("password"); var myAccount = orbiter.self().getAccount(); if (myAccount != null) { myAccount.logoff(userid.value, password.value) } else { displayMessage("Not logged in. Login before attempting to log off.") } } </script> </head> <body onload="init()"> <!--Contains the output pane--> <div id="outPane"></div> <!--Inputs--> User ID: <input type="text" id="userid" style="width:140px"/><br> Password: <input type="text" id="password" style="width:140px"/><br> <input type="submit" value="Create Account" onclick="createAccount()"/> <input type="submit" value="Login" onclick="login()"/> <input type="submit" value="Logoff" onclick="logoff()"/> </body> </html>