FMS helloworld

本文介绍了一个使用ActionScript 3.0 (AS3) 实现的Flash网络通信示例。该示例通过创建一个简单的客户端应用,演示了如何连接到服务器并发送消息,服务器接收到消息后会返回一个响应。此过程涉及客户端与服务器间的连接建立、消息发送及接收等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值