package { import flash . display . * ; import flash . events . * ; import flash . net . URLRequest ; import flash . text . * ; import com . SocketsConnection . XMLSocketModel ; import com . SocketsConnection . XMLSocketView ; import com . SocketsConnection . XMLSocketController ; import com . Red5Link . Red5Broadcast ; public class ClientDocument extends MovieClip { public var xmlSocketModel : XMLSocketModel ; public var xmlSocketView : XMLSocketView ; public var xmlSocketController : XMLSocketController ; public var broadcast : Red5Broadcast ; public function ClientDocument( ) { msgArea . visible = false ; inputMessage . visible = false ; winnerStamp . visible = false ; broadcast = new Red5Broadcast ( " rtmp://yourdomain.com " , " red5BroadcastDemo " , red5StatusTxt ) ; addChild ( broadcast ) ; xmlSocketModel = new XMLSocketModel ( " arduinoproject.no-ip.org " , 8890 , " client " ) ; xmlSocketView = new XMLSocketView ( msgArea , socketStatusTxt ) ; xmlSocketView . socketsModel = xmlSocketModel ; xmlSocketController = new XMLSocketController ( ) ; xmlSocketController . socketsModel = xmlSocketModel ; xmlSocketController . videoBroadcast = broadcast ; xmlSocketController . addSendButton ( sendButton , inputMessage ) ; xmlSocketController . addReconnectButton ( reconnectButton ) ; xmlSocketController . addMotorButtons ( motorX , motorY , motorZ ) ; xmlSocketController . addQueueStatus ( queueStatus ) ; xmlSocketController . addWinnerStamp ( winnerStamp ) ; xmlSocketController . sharedObject = new Array ( " rtmp://yourdomain.com " , " motors " , sharedStatusTxt ) ; } } }

public function ClientDocument( ) { msgArea . visible = false ; inputMessage . visible = false ; winnerStamp . visible = false ; broadcast = new Red5Broadcast ( " rtmp://yourdomain.com " , " red5BroadcastDemo " , red5StatusTxt ) ; addChild ( broadcast ) ; xmlSocketModel = new XMLSocketModel ( " arduinoproject.no-ip.org " , 8890 , " client " ) ; xmlSocketView = new XMLSocketView ( msgArea , socketStatusTxt ) ; xmlSocketView . socketsModel = xmlSocketModel ; xmlSocketController = new XMLSocketController ( ) ; xmlSocketController . socketsModel = xmlSocketModel ; xmlSocketController . videoBroadcast = broadcast ; xmlSocketController . addSendButton ( sendButton , inputMessage ) ; xmlSocketController . addReconnectButton ( reconnectButton ) ; xmlSocketController . addMotorButtons ( motorX , motorY , motorZ ) ; xmlSocketController . addQueueStatus ( queueStatus ) ; xmlSocketController . addWinnerStamp ( winnerStamp ) ; xmlSocketController . sharedObject = new Array ( " rtmp://yourdomain.com " , " motors " , sharedStatusTxt ) ; }

xmlSocketModel = new XMLSocketModel ( " arduinoproject.no-ip.org " , 8890 , " client " ) ;

xmlSocketView = new XMLSocketView ( msgArea ) ;

xmlSocketView . socketsModel = xmlSocketModel ;

xmlSocketController = new XMLSocketController ( ) ; xmlSocketController . socketsModel = xmlSocketModel ; xmlSocketController . videoBroadcast = broadcast ; xmlSocketController . addSendButton ( sendButton , inputMessage ) ; xmlSocketController . addReconnectButton ( reconnectButton ) ; xmlSocketController . addMotorButtons ( motorX , motorY , motorZ ) ; xmlSocketController . addQueueStatus ( queueStatus ) ; xmlSocketController . addWinnerStamp ( winnerStamp ) ; xmlSocketController . sharedObject = new Array ( " rtmp://yourdomain.com " , " motors " , sharedStatusTxt ) ;

package { import flash . display . * ; import flash . events . * ; import flash . net . URLRequest ; import flash . text . * ; import fl . controls . Slider ; import com . SocketsConnection . XMLSocketModel ; import com . SocketsConnection . XMLSocketView ; import com . SocketsConnection . XMLSocketAppController ; import com . SocketsConnection . SocketModel ; import com . SocketsConnection . SocketView ; import com . SocketsConnection . SocketAppController ; import com . Red5Link . Red5Broadcast ; public class ArduinoAIRDocument extends MovieClip { public var xmlSocketModel : XMLSocketModel ; public var xmlSocketView : XMLSocketView ; public var xmlSocketAppController : XMLSocketAppController ; public var socketModel : SocketModel ; public var socketView : SocketView ; public var socketAppController : SocketAppController ; public var broadcast : Red5Broadcast ; public function ArduinoAIRDocument( ) { broadcast = new Red5Broadcast ( " rtmp://yourdomain.com " , " red5BroadcastDemo " , red5StatusTxt ) ; addChild ( broadcast ) ; xmlSocketModel = new XMLSocketModel ( " localhost " , 8890 , " AIR " ) ; xmlSocketView = new XMLSocketView ( msgArea , socketStatusTxt ) ; xmlSocketView . socketsModel = xmlSocketModel ; socketModel = new SocketModel("localhost", 5333,"arduino"); socketView = new SocketView ( msgArea ) ; socketView . socketsModel = socketModel ; xmlSocketAppController = new XMLSocketAppController ( ) ; xmlSocketAppController . socketsModel = xmlSocketModel ; xmlSocketAppController . colaboratingModel = socketModel ; xmlSocketAppController . videoBroadcast = broadcast ; xmlSocketAppController . addSendButton ( sendButton , inputMessage ) ; xmlSocketAppController . addReconnectButton ( reconnectButton ) ; xmlSocketAppController . addSendArduinoButton ( sendArduinoButton ) ; xmlSocketAppController . addMotorButtons ( motorX , motorY , motorZ ) ; xmlSocketAppController . sharedObject = new Array ( " rtmp://yourdomain.com " , " motors " , sharedStatusTxt ) ; socketAppController = new SocketAppController ( ) ; socketAppController . socketsModel = socketModel ; socketAppController . colaboratingModel = xmlSocketModel ; } } }

public function ArduinoAIRDocument( ) { broadcast = new Red5Broadcast ( " rtmp://yourdomain.com " , " red5BroadcastDemo " , red5StatusTxt ) ; addChild ( broadcast ) ; xmlSocketModel = new XMLSocketModel ( " localhost " , 8890 , " AIR " ) ; xmlSocketView = new XMLSocketView ( msgArea , socketStatusTxt ) ; xmlSocketView . socketsModel = xmlSocketModel ; socketModel = new SocketModel("localhost", 5333,"arduino"); socketView = new SocketView ( msgArea ) ; socketView . socketsModel = socketModel ; xmlSocketAppController = new XMLSocketAppController ( ) ; xmlSocketAppController . socketsModel = xmlSocketModel ; xmlSocketAppController . colaboratingModel = socketModel ; xmlSocketAppController . videoBroadcast = broadcast ; xmlSocketAppController . addSendButton ( sendButton , inputMessage ) ; xmlSocketAppController . addReconnectButton ( reconnectButton ) ; xmlSocketAppController . addSendArduinoButton ( sendArduinoButton ) ; xmlSocketAppController . addMotorButtons ( motorX , motorY , motorZ ) ; xmlSocketAppController . sharedObject = new Array ( " rtmp://yourdomain.com " , " motors " , sharedStatusTxt ) ; socketAppController = new SocketAppController ( ) ; socketAppController . socketsModel = socketModel ; socketAppController . colaboratingModel = xmlSocketModel ; }

socketModel = new SocketModel ( " localhost " , 5333 , " arduino " ) ; socketView = new SocketView ( msgArea ) ; socketView . socketsModel = socketModel ;

Now that we have our socket ready we can get down and dirty with the ActionScript 3.0 code. You can download the code at the end of this step.After giving it a lot of thought I decided not to go to detail and explain how I wrote the class and what it does. The scope of this instructable is not to teach you about classes, design patterns and OOP(object oriented programming). I'll rather focus on how to use it. When I started coding I decided to use a MVC (Model/View/Controller) design pattern for the class and as it grew a realized that maybe it wasn't the best idea and that I should have used another more simple and scalable design pattern. But being eager to continue with the project I decided to stick to it. Don't worry the class will work anyway. I'll be posting the new classes as soon as I finish them.So open Flash and create two new flash files. One as ActionScript 3.0 and name it Client.fla, and the other for Adobe AIR with the name ArduinoAIR.fla. Then create two ActionScript files and call them ClienDocument.as and ArduinoAIRDocument.as .The client contains a TextArea component named “msgArea”, InputText component named “inputMessage”, two buttons component named “sendButton” and “reconnectButton”, three slider components called “motorX”, “motorY”, “motorZ”, and four dynamic TextFields called "red5StatusTxt", "sharedStatusTxt","socketStatusTxt" and "queueStatus".The AIR app has the same components, except it also has one extra button component called “sendArduinoButton”. When you have these set up you have to point each flash file to it's document class. Document classes in plain terms are used to move your code from the timeline to an external acrionscript file. So for the Client.fla the document class should be ClientDocument.as, and for ArduinoAIR.fla should be ArduinoAIRDocument.as .OK. Now open ClientDocument.as and lest see what we've got here.In the first few lines we're defining the classes we're going to use.The things we are interested are inside the public funcionso lets go through each of them and explain what they do.The first three lines hide the TextAreas and winner trophy. The 4. line initializes theclass that handles the streaming and broadcasting of the video. We have to give it the location of the red5 server, the red5 application name and the TextField in which the stream status is displayed. Then we have to add it to the display list otherwise it wouldn't show the video stream.Next, Flash communicates with the php socket over an XMLSocket class that is part of flash. I created a Model class that will handle all the necessary listeners and dispatch events when the data is received. It is calledand we initialize it this way:We have to pass the URL where is our socket, our socket number, and as the last parameter the app type. The URL I use is arduinoproject.no-ip.org . Yes I use www.no-ip.org service. This way I have the possibility to have a hostname for my dynamic IP. Just register, pick a host name and install their program on your computer. The last thing we pass is "client" because we are now working on our client flash file. Note that all of the parameters must be filled in.The next line initializes the View class. It is used to display the incoming data and it changes when it receives the events dispatched from the Model class.When we initialize thewe pass to it the TextArea called “msgArea” as a parameter. That tells it where to display our incoming data. The next line tells which Model it should listen for events.So xmlSocketView should listen xmlSocketModel for events.Next we have to initialize our Controller class. This is where all of the magic happens. The Controller class is where all the decisions and are made and all of the command are issued. Also the buttons that we put on the stage are going to have listeners in the controller firing commands when we press them, the controller also changes when it receives the events dipatched from the Model class. So, first we initialize the Controler. In the next line we tell it which Model it should listen to. Again as with the View class we tell it the Model is xmlSocketModel .Then we tell it that it's. We add the buttons, the "queueStatus" TextField for displaying the queue, the "winnerStamp" MovieClip that shows if the client wins.Finaly we initialize thewhich will exchange the commands for the motor with the AIR app. The parameters are your domain path, the name for the shared object and the "sharedStatusTxt" TextField where the status of the shared connection is displayed.Now we're going to take a look at the code for the AIR app.Take a careful look at public function ArduinoAIRDocument().The code is almost the same as in the client flash file except when we initialize the xmlSocketModel class we have to pass “AIR” as the last parameter.The new thing that we have here is happening in lines 8 through 10.Here we initialize the. The socketModel is the Model class for communicating with arduino. It uses flash's Socket class. Line 9 initializes thewhich is the View class for the socketModel. When we initialze it we have to pass the TextArea “msgArea” because that is also where we'll display the commands we send to arduino and what arduino returns to the AIR app. Line 10 tells the socketView to listen for events from the socketModel.Theextends from theso we initialize it almost the same way.The last three lines of code initialize thewhich is the controller for the socketModel. It will handle the communication between the AIR app and arduino through port 5333. Port 5333 will be opened by TinkerProxy.Now we have to publish our files. Publish the Client.fla for Flash Player 10 and the AIR app.