Flex连接FMS实现用户列表(使用SharedObject)

1.先在FMS的安装目录中添加自己的应用(例如我的目录D:\Macromedia\Flash Media Server 2\applications),在applications目录下建立文件夹userList。

2.用文本编辑器编写服务器端脚本main.asc文件,内容如下:

js 代码
  1. application.onAppStart = function()   
  2. {   
  3.     userList_so = SharedObject.get ("userList","false");   
  4. }   
  5. application.onConnect = function(client,userName)   
  6. {   
  7.     client.userName = userName;   
  8.     userList_so.setProperty (userName, userName);   
  9.     return true;   
  10. }   
  11.   
  12. application.onDisconnect = function(client)   
  13. {   
  14.     userList_so.setProperty(client.userName,null);   
  15. }  

 

3.编写客户端应用userList.mxml,内容如下:

xml 代码
  1. <!---->xml version="1.0" encoding="utf-8"?>  
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initApp();">  
  3.     <mx:Label x="79" y="63" text="User Name:" fontWeight="bold" fontStyle="italic"/>  
  4.     <mx:TextInput x="155" y="61" id="txtUserName"/>  
  5.     <mx:Button x="82" y="89" label="Connect To Flash Media Server" width="233" fontWeight="bold" fontStyle="italic" click="connectServer();"/>  
  6.     <mx:List x="82" y="145" width="233" height="252" id="listUserList" labelField="userName">mx:List>  
  7.     <mx:Label x="82" y="119" text="User List:" fontWeight="bold" fontStyle="italic"/>  
  8.     <mx:Script>  
  9.         <!----> 
  10.             import flash.net.*;  
  11.             import mx.controls.*;  
  12.             import flash.events.*;  
  13.             import mx.collections.ArrayCollection;  
  14.               
  15.             private var conn:NetConnection;  
  16.             private var sharedObject:SharedObject;  
  17.               
  18.             NetConnection.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;  
  19.             SharedObject.defaultObjectEncoding  = flash.net.ObjectEncoding.AMF0;  
  20.               
  21.             private function initApp():void  
  22.             {  
  23.                 conn = new NetConnection();  
  24.                 conn.addEventListener(NetStatusEvent.NET_STATUS, netStatus);  
  25.                 conn.addEventListener(SecurityErrorEvent.SECURITY_ERROR, netSecurityError);  
  26.             }  
  27.               
  28.             private function netStatus(event:NetStatusEvent):void  
  29.             {  
  30.                 var connStatus:String = event.info.code;  
  31.                 Alert.show(connStatus);  
  32.                 if(connStatus == "NetConnection.Connect.Success")  
  33.                 {  
  34.                     sharedObject = SharedObject.getRemote("userList", conn.uri, false);  
  35.                     sharedObject.addEventListener(SyncEvent.SYNC, soOnSync);  
  36.                     sharedObject.connect(conn);  
  37.                     listUserList.dataProvider = new ArrayCollection();  
  38.                 }  
  39.             }  
  40.               
  41.             private function soOnSync(event:SyncEvent):void   
  42.             {  
  43.                 var dp:ArrayCollection = listUserList.dataProvider as ArrayCollection;  
  44.                 dp.removeAll();  
  45.                 for(var i:String in sharedObject.data)   
  46.                 {  
  47.                     var item:Object = {userName:i,data:i};  
  48.                     dp.addItem(item);  
  49.                 }  
  50.                 listUserList.dataProvider = dp;  
  51.             }  
  52.               
  53.             private function netSecurityError(event:SecurityErrorEvent):void   
  54.             {  
  55.                 Alert.show("连接到服务器发生错误!"+event.text);  
  56.             }  
  57.               
  58.             private function connectServer():void  
  59.             {  
  60.                 Alert.show(txtUserName.text+"正在登录...");  
  61.                 conn.connect("rtmp://localhost/userList",txtUserName.text);  
  62.             }  
  63.               
  64.         ]]>  
  65.     mx:Script>  
  66. mx:Application>  

你可能感兴趣的:(.net,应用服务器,Flex,Flash,Adobe)