借用下网上一个例子,一个简单的客户端通过访问服务器具有涂鸦功能的应用。略微修改。
1.客户端
package { import flash.display.Sprite; import flash.events.MouseEvent; import flash.events.NetStatusEvent; import flash.events.SyncEvent; import flash.net.NetConnection; import flash.net.SharedObject; import flash.text.TextField; import flash.text.TextFieldAutoSize; import fl.events.ColorPickerEvent; import flash.events.Event; import flash.geom.ColorTransform; import flash.net.URLRequest; import flash.display.Loader; public class RemoteStore extends Sprite { private var remoteStore:SharedObject; private var con:NetConnection; private var pos:Array; private var isDrawing:Boolean = false; private var lineColor:uint; private var lineThickness:Number = 1.0; private var points:Array; private var isFirstSync:Boolean = true; private var sprite:Sprite=new Sprite(); private var UserName:String = "123"; public function RemoteStore() { mog_lb.text = "正在连接服务器...."; this.addChild(mog_lb); var picreq:URLRequest = new URLRequest("ms.jpg"); var pic:Loader=new Loader(); pic.load(picreq); bank.addChild(pic); bank.addChild(sprite); con=new NetConnection(); var clientObj:Object = new Object();//flash.net.NetConnection 无法调用回调 onBWDone。 clientObj.WB = WB; con.client = clientObj; con.connect("rtmp://localhost/RemoteStore"); con.addEventListener(NetStatusEvent.NET_STATUS,onConNetStatu); } public function onConNetStatu(e:NetStatusEvent):void { if (e.info.code == "NetConnection.Connect.Success") { remoteStore = SharedObject.getRemote("DrawDemo",con.uri,false); remoteStore.connect(con); remoteStore.fps = 10; remoteStore.data.pos = points; remoteStore.client = this; remoteStore.addEventListener(SyncEvent.SYNC,onSynchandler); mog_lb.text = "成功连接服务器..."; trace("NetConnection.Connect.Success"); } if (e.info.code == "NetConnection.Connect.Failed") { mog_lb.text = "连接服务器失败.."; } } public function onSynchandler(e:SyncEvent):void { if (isFirstSync) { this.bank.addEventListener(MouseEvent.MOUSE_MOVE,onmouseMove); this.bank.addEventListener(MouseEvent.MOUSE_DOWN,onmouseDown); this.bank.addEventListener(MouseEvent.MOUSE_UP,onmouseUp); cleaner.addEventListener(MouseEvent.CLICK,clicker); btn.addEventListener(MouseEvent.CLICK,clicker1); } if (isDrawing) { return; } if (remoteStore.data.pos != null) { this.sprite.graphics.lineStyle(lineThickness,lineColor); var pos:Array = remoteStore.data.pos as Array; for (var i:int=0; i<pos.length; i++) { if (i == 0) { this.sprite.graphics.moveTo(pos[i].x,pos[i].y); } else { this.sprite.graphics.lineTo(pos[i].x,pos[i].y); } } } } public function onmouseUp(e:MouseEvent):void { remoteStore.setProperty("pos",points); isDrawing = false; remoteStore.send("setLineStyle",lineThickness,lineColor,colorTran); } public function onmouseDown(e:MouseEvent):void { getcolor.addEventListener(ColorPickerEvent.CHANGE,changeHandler); getnum.addEventListener(Event.CHANGE, changeNum); e.updateAfterEvent(); this.sprite.graphics.lineStyle(lineThickness,lineColor); points=new Array(); this.sprite.graphics.moveTo(e.localX,e.localY); points.push({x:e.localX,y:e.localY}); } public function onmouseMove(e:MouseEvent):void { if (e.buttonDown) { this.sprite.graphics.lineTo(e.localX,e.localY); var p:Object = {x:e.localX,y:e.localY}; points.push(p); isDrawing = true; e.updateAfterEvent(); } } public function setLineStyle(thickness:Number,clr:uint,col:uint):void { colorTran = col; lineThickness = thickness; lineColor = clr; } public function changeHandler(e:ColorPickerEvent):void { lineColor = e.target.selectedColor; } public function changeNum(event:Event):void { lineThickness = event.target.value; } public function clicker(event:MouseEvent):void { this.sprite.graphics.clear(); con.call("clearWB", null,UserName); } public function clicker1(event:MouseEvent):void { this.sprite.graphics.clear(); con.call("clearWB", null,UserName); } public function WB(UserName:Object):void { trace("------WB--------"); this.sprite.graphics.clear(); } } }
application.onAppStart = function() { trace("onAppStart"); }; application.onConnect = function(client, uName) { trace("onConnect = "+uName); client.UserName = uName; application.acceptConnection(client); //接收客户端传来的数据 (跟客户端的 client_nc.call 相对应) client.clearWB= function(aa) { trace(aa) application.broadcastMsg("WB", aa); }; }; application.onAppStop = function() { trace("onAppStop"); };
3、所存问题
问题:FMS设置完好,端口也已经开放,可连接上服务器,但是访问不到main.asc文件。
解答:网上的一些例子都是基于fms3.0。当时装的是FMS4.0试用版本,所以没有访问成功。以为要重装为fms3.0,无意间在天地会上发现了一种方法,竟然解决了。方法如下:安装FMS后,必须在C:\Windows\system32\drivers\etc\hosts,也就是打开host这个文件,用记事本程序打开,在最后一行,加入:127.0.0.1 activate.adobe.com 。重启服务器,搞定!