·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[AS3]as3与FMS4.0客户端访问服务器脚本文件main.asc

时间:2014-10-27 09:04酷播
[AS3]as3与FMS4.0客户端访问服务器脚本文件main.asc

借用下网上一个例子,一个简单的客户端通过访问服务器具有涂鸦功能的应用。略微修改。

1.客户端

  1. package  
  2.     import flash.display.Sprite; 
  3.     import flash.events.MouseEvent; 
  4.     import flash.events.NetStatusEvent; 
  5.     import flash.events.SyncEvent; 
  6.     import flash.net.NetConnection; 
  7.     import flash.net.SharedObject; 
  8.     import flash.text.TextField; 
  9.     import flash.text.TextFieldAutoSize; 
  10.     import fl.events.ColorPickerEvent; 
  11.     import flash.events.Event; 
  12.     import flash.geom.ColorTransform; 
  13.     import flash.net.URLRequest; 
  14.     import flash.display.Loader; 
  15.     public class RemoteStore extends Sprite 
  16.     { 
  17.         private var remoteStore:SharedObject; 
  18.         private var con:NetConnection; 
  19.         private var pos:Array; 
  20.         private var isDrawing:Boolean = false
  21.         private var lineColor:uint; 
  22.         private var lineThickness:Number = 1.0; 
  23.         private var points:Array; 
  24.         private var isFirstSync:Boolean = true
  25.         private var sprite:Sprite=new Sprite(); 
  26.  
  27.         private var UserName:String = "123"
  28.  
  29.         public function RemoteStore() 
  30.         { 
  31.             mog_lb.text = "正在连接服务器...."
  32.             this.addChild(mog_lb); 
  33.             var picreq:URLRequest = new URLRequest("ms.jpg"); 
  34.             var pic:Loader=new Loader(); 
  35.             pic.load(picreq); 
  36.             bank.addChild(pic); 
  37.             bank.addChild(sprite); 
  38.             con=new NetConnection(); 
  39.             var clientObj:Object = new Object();//flash.net.NetConnection 无法调用回调 onBWDone。 
  40.             clientObj.WB = WB; 
  41.             con.client = clientObj
  42.             con.connect("rtmp://localhost/RemoteStore"); 
  43.  
  44.             con.addEventListener(NetStatusEvent.NET_STATUS,onConNetStatu); 
  45.         } 
  46.         public function onConNetStatu(e:NetStatusEvent):void 
  47.         { 
  48.             if (e.info.code == "NetConnection.Connect.Success") 
  49.             { 
  50.                 remoteStore = SharedObject.getRemote("DrawDemo",con.uri,false); 
  51.                 remoteStore.connect(con); 
  52.                 remoteStore.fps = 10
  53.                 remoteStore.data.pos = points
  54.                 remoteStore.client = this
  55.                 remoteStore.addEventListener(SyncEvent.SYNC,onSynchandler); 
  56.                 mog_lb.text = "成功连接服务器..."
  57.                 trace("NetConnection.Connect.Success"); 
  58.  
  59.             } 
  60.             if (e.info.code == "NetConnection.Connect.Failed") 
  61.             { 
  62.                 mog_lb.text = "连接服务器失败.."
  63.             } 
  64.         } 
  65.         public function onSynchandler(e:SyncEvent):void 
  66.         { 
  67.             if (isFirstSync) 
  68.             { 
  69.                 this.bank.addEventListener(MouseEvent.MOUSE_MOVE,onmouseMove); 
  70.                 this.bank.addEventListener(MouseEvent.MOUSE_DOWN,onmouseDown); 
  71.                 this.bank.addEventListener(MouseEvent.MOUSE_UP,onmouseUp); 
  72.                 cleaner.addEventListener(MouseEvent.CLICK,clicker); 
  73.                  
  74.                 btn.addEventListener(MouseEvent.CLICK,clicker1); 
  75.             } 
  76.             if (isDrawing) 
  77.             { 
  78.                 return; 
  79.             } 
  80.  
  81.             if (remoteStore.data.pos != null) 
  82.             { 
  83.                 this.sprite.graphics.lineStyle(lineThickness,lineColor); 
  84.                 var pos:Array = remoteStore.data.pos as Array; 
  85.                 for (var i:int=0; i<pos.length; i++) 
  86.                 { 
  87.                     if (i == 0) 
  88.                     { 
  89.                         this.sprite.graphics.moveTo(pos[i].x,pos[i].y); 
  90.                     } 
  91.                     else 
  92.                     { 
  93.                         this.sprite.graphics.lineTo(pos[i].x,pos[i].y); 
  94.                     } 
  95.                 } 
  96.             } 
  97.         } 
  98.         public function onmouseUp(e:MouseEvent):void 
  99.         { 
  100.             remoteStore.setProperty("pos",points); 
  101.             isDrawing = false
  102.             remoteStore.send("setLineStyle",lineThickness,lineColor,colorTran); 
  103.         } 
  104.         public function onmouseDown(e:MouseEvent):void 
  105.         { 
  106.             getcolor.addEventListener(ColorPickerEvent.CHANGE,changeHandler); 
  107.             getnum.addEventListener(Event.CHANGE, changeNum); 
  108.             e.updateAfterEvent(); 
  109.             this.sprite.graphics.lineStyle(lineThickness,lineColor); 
  110.             points=new Array(); 
  111.             this.sprite.graphics.moveTo(e.localX,e.localY); 
  112.             points.push({x:e.localX,y:e.localY}); 
  113.         } 
  114.         public function onmouseMove(e:MouseEvent):void 
  115.         { 
  116.             if (e.buttonDown) 
  117.             { 
  118.                 this.sprite.graphics.lineTo(e.localX,e.localY); 
  119.                 var p:Object = {x:e.localX,y:e.localY}; 
  120.                 points.push(p); 
  121.                 isDrawing = true
  122.                 e.updateAfterEvent(); 
  123.             } 
  124.         } 
  125.         public function setLineStyle(thickness:Number,clr:uint,col:uint):void 
  126.         { 
  127.             colcolorTran = col; 
  128.             lineThickness = thickness
  129.             lineColor = clr
  130.         } 
  131.         public function changeHandler(e:ColorPickerEvent):void 
  132.         { 
  133.             lineColor = e.target.selectedColor; 
  134.         } 
  135.         public function changeNum(event:Event):void 
  136.         { 
  137.             lineThickness = event.target.value; 
  138.         } 
  139.         public function clicker(event:MouseEvent):void 
  140.         { 
  141.             this.sprite.graphics.clear(); 
  142.             con.call("clearWB", null,UserName); 
  143.         } 
  144.          
  145.         public function clicker1(event:MouseEvent):void 
  146.         { 
  147.             this.sprite.graphics.clear(); 
  148.             con.call("clearWB", null,UserName); 
  149.         } 
  150.          
  151.         public function WB(UserName:Object):void 
  152.         { 
  153.             trace("------WB--------"); 
  154.             this.sprite.graphics.clear(); 
  155.         } 
  156.     } 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器