FMS helloworld
2011年05月17日
Start Flash and select Create New > Flash File (ActionScript 3.0). In the Document Class field, enter HelloWorld. If you see an ActionScript Class Warning message about a missing definition-click OK. You will add the class file in the next section. Choose Windows > Components. Click User Interface and double-click Button to add it to the Stage. On the Properties tab, enter the instance name connectBtn. Add a Label component above the button, and give it the instance name textLbl. Save the file as HelloWorld.fla. You can save the client files to any location. This script provides two button actions, either connecting to or disconnecting from the server. When connecting, the script calls the server with a string ("World"), which triggers a response that displays the returned string ("Hello, World!"). Choose File > New > ActionScript File. Check that the Target box has HelloWorld.fla. Declare the package and import the required Flash classes: package { import flash.display.MovieClip; import flash.net.Responder; import flash.net.NetConnection; import flash.events.NetStatusEvent; import flash.events.MouseEvent; public class HelloWorld extends MovieClip { } }
Inside the HelloWorld class declaration, declare variables for the connection and the server responder: private var nc:NetConnection; private var myResponder:Responder = new Responder(onReply);
Define the class constructor. Set the label and button display values, and add an event listener to the button: public function HelloWorld() { textLbl.text = ""; connectBtn.label = "Connect"; connectBtn.addEventListener(MouseEvent.CLICK, connectHandler); }
Define the event listener actions, which depend on the button's current label: public function connectHandler(event:MouseEvent):void { if (connectBtn.label == "Connect") { trace("Connecting..."); nc = new NetConnection(); // Connect to the server. nc.connect("rtmp://localhost/HelloWorld"); // Call the server's client function serverHelloMsg, in HelloWorld.asc. nc.call("serverHelloMsg", myResponder, "World"); connectBtn.label = "Disconnect"; } else { trace("Disconnecting..."); // Close the connection. nc.close(); connectBtn.label = "Connect"; textLbl.text = ""; } }
Define the responder function, which sets the label's display value: private function onReply(result:Object):void { trace("onReply received value: " + result); textLbl.text = String(result); }
Save the file as HelloWorld.as to the same folder as the HelloWorld.fla file. Choose File > New > ActionScript Communications File. Define the server-side function and the connection logic: application.onConnect = function( client ) { client.serverHelloMsg = function( helloStr ) { return "Hello, " + helloStr + "!"; } application.acceptConnection( client ); }
Save the file as HelloWorld.asc in the rootinstall/applications/HelloWorld folder. (Create the "HelloWorld" folder when you save the file.) Verify that the server is running. Select the HelloWorld.fla file tab. Choose Control > Test Movie. Click the Connect button. "Hello, World!" is displayed, and the button label changes to Disconnect. Click the Disconnect button. The output of the trace() statements is displayed in the Flash Output window.
2011年05月17日
Start Flash and select Create New > Flash File (ActionScript 3.0). In the Document Class field, enter HelloWorld. If you see an ActionScript Class Warning message about a missing definition-click OK. You will add the class file in the next section. Choose Windows > Components. Click User Interface and double-click Button to add it to the Stage. On the Properties tab, enter the instance name connectBtn. Add a Label component above the button, and give it the instance name textLbl. Save the file as HelloWorld.fla. You can save the client files to any location. This script provides two button actions, either connecting to or disconnecting from the server. When connecting, the script calls the server with a string ("World"), which triggers a response that displays the returned string ("Hello, World!"). Choose File > New > ActionScript File. Check that the Target box has HelloWorld.fla. Declare the package and import the required Flash classes: package { import flash.display.MovieClip; import flash.net.Responder; import flash.net.NetConnection; import flash.events.NetStatusEvent; import flash.events.MouseEvent; public class HelloWorld extends MovieClip { } }
Inside the HelloWorld class declaration, declare variables for the connection and the server responder: private var nc:NetConnection; private var myResponder:Responder = new Responder(onReply);
Define the class constructor. Set the label and button display values, and add an event listener to the button: public function HelloWorld() { textLbl.text = ""; connectBtn.label = "Connect"; connectBtn.addEventListener(MouseEvent.CLICK, connectHandler); }
Define the event listener actions, which depend on the button's current label: public function connectHandler(event:MouseEvent):void { if (connectBtn.label == "Connect") { trace("Connecting..."); nc = new NetConnection(); // Connect to the server. nc.connect("rtmp://localhost/HelloWorld"); // Call the server's client function serverHelloMsg, in HelloWorld.asc. nc.call("serverHelloMsg", myResponder, "World"); connectBtn.label = "Disconnect"; } else { trace("Disconnecting..."); // Close the connection. nc.close(); connectBtn.label = "Connect"; textLbl.text = ""; } }
Define the responder function, which sets the label's display value: private function onReply(result:Object):void { trace("onReply received value: " + result); textLbl.text = String(result); }
Save the file as HelloWorld.as to the same folder as the HelloWorld.fla file. Choose File > New > ActionScript Communications File. Define the server-side function and the connection logic: application.onConnect = function( client ) { client.serverHelloMsg = function( helloStr ) { return "Hello, " + helloStr + "!"; } application.acceptConnection( client ); }
Save the file as HelloWorld.asc in the rootinstall/applications/HelloWorld folder. (Create the "HelloWorld" folder when you save the file.) Verify that the server is running. Select the HelloWorld.fla file tab. Choose Control > Test Movie. Click the Connect button. "Hello, World!" is displayed, and the button label changes to Disconnect. Click the Disconnect button. The output of the trace() statements is displayed in the Flash Output window.