package { // Class so we can display and/or be used in the Flash IDE import flash.display.Sprite; // Class that allows a Shared Object to exist on FMS import flash.net.SharedObject; // So we can get Status from the NetConnection (callback) import flash.events.NetStatusEvent; // So we can get events from the SharedObject (callback) import flash.events.SyncEvent; // So we can connect to FMS import flash.net.NetConnection; // Import TextField to show what to send/recieve import flash.text.TextField; import flash.text.TextFieldType; // Import TextEvent so we know when something happens in TextField import flash.events.TextEvent; public class SimpleSO extends Sprite { // Declare a SharedObject object private var sharedObject:SharedObject; // Declare a NetConnection Object private var netConnection:NetConnection; // Declare a TextField private var textField:TextField; // Constructor public function SimpleSO() { trace("starting"); // String with URL to app on FMS var rtmpURL:String = "rtmp://itp-flash.es.its.nyu.edu/sve204/SimpleSO"; // Instantiate NetConnection netConnection = new NetConnection(); // Event listener, calls netStatusCallBack when a NetStatusEvent is fired netConnection.addEventListener(NetStatusEvent.NET_STATUS,netStatusCallBack); // Connect to the application on the server netConnection.connect(rtmpURL); // Create the textField textField = new TextField(); // Add event listener textField.addEventListener(TextEvent.TEXT_INPUT, textEventCallBack); textField.type = TextFieldType.INPUT; textField.x = 10; textField.y = 10; textField.width = 100; textField.height = 40; textField.text = "hi"; addChild(textField); } // NetStatusEvent Callback private function netStatusCallBack(nsEvent:NetStatusEvent):void { // If we connected successfully if (nsEvent.info.code == "NetConnection.Connect.Success") { // Set up the shared object // We'll call it SimpleSO, pass in the app url and not make it persistent sharedObject = SharedObject.getRemote("SimpleSO",netConnection.uri,false); // Add a listener for when shared object is changed sharedObject.addEventListener (SyncEvent.SYNC,syncEventCallBack); // Connect the shared object to our netConnection sharedObject.connect(netConnection); } else { // Didn't connect trace("Sorry connection failed"); } } private function syncEventCallBack(syncEvent:SyncEvent):void { // Array of changes: syncEvent.changeList[] for (var i:int = 0; i < syncEvent.changeList.length; i++) { trace(syncEvent.changeList[i].code); switch(syncEvent.changeList[i].code) { case "clear": break; case "success": break; case "change": var newData:String = sharedObject.data.theData; trace("recieved " + newData); // Update TextField textField.text = sharedObject.data.theData; break; } } } private function textEventCallBack(textEvent:TextEvent):void { trace(textEvent.text); if (textEvent.text == " ") { sendData(textField.text); } } private function sendData(dataToSend:String):void { sharedObject.setProperty("theData",dataToSend); } } }