1.先在FMS的安装目录中添加自己的应用(例如我的目录D:\Macromedia\Flash Media Server 2\applications),在applications目录下建立文件夹userList。
2.用文本编辑器编写服务器端脚本main.asc文件,内容如下:
js 代码
- application.onAppStart = function()
- {
- userList_so = SharedObject.get ("userList","false");
- }
- application.onConnect = function(client,userName)
- {
- client.userName = userName;
- userList_so.setProperty (userName, userName);
- return true;
- }
- application.onDisconnect = function(client)
- {
- userList_so.setProperty(client.userName,null);
- }
3.编写客户端应用userList.mxml,内容如下:
xml 代码
- xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initApp();">
- <mx:Label x="79" y="63" text="User Name:" fontWeight="bold" fontStyle="italic"/>
- <mx:TextInput x="155" y="61" id="txtUserName"/>
- <mx:Button x="82" y="89" label="Connect To Flash Media Server" width="233" fontWeight="bold" fontStyle="italic" click="connectServer();"/>
- <mx:List x="82" y="145" width="233" height="252" id="listUserList" labelField="userName">mx:List>
- <mx:Label x="82" y="119" text="User List:" fontWeight="bold" fontStyle="italic"/>
- <mx:Script>
- import flash.net.*;
- import mx.controls.*;
- import flash.events.*;
- import mx.collections.ArrayCollection;
- private var conn:NetConnection;
- private var sharedObject:SharedObject;
- NetConnection.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;
- SharedObject.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;
- private function initApp():void
- {
- conn = new NetConnection();
- conn.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
- conn.addEventListener(SecurityErrorEvent.SECURITY_ERROR, netSecurityError);
- }
- private function netStatus(event:NetStatusEvent):void
- {
- var connStatus:String = event.info.code;
- Alert.show(connStatus);
- if(connStatus == "NetConnection.Connect.Success")
- {
- sharedObject = SharedObject.getRemote("userList", conn.uri, false);
- sharedObject.addEventListener(SyncEvent.SYNC, soOnSync);
- sharedObject.connect(conn);
- listUserList.dataProvider = new ArrayCollection();
- }
- }
- private function soOnSync(event:SyncEvent):void
- {
- var dp:ArrayCollection = listUserList.dataProvider as ArrayCollection;
- dp.removeAll();
- for(var i:String in sharedObject.data)
- {
- var item:Object = {userName:i,data:i};
- dp.addItem(item);
- }
- listUserList.dataProvider = dp;
- }
- private function netSecurityError(event:SecurityErrorEvent):void
- {
- Alert.show("连接到服务器发生错误!"+event.text);
- }
- private function connectServer():void
- {
- Alert.show(txtUserName.text+"正在登录...");
- conn.connect("rtmp://localhost/userList",txtUserName.text);
- }
- ]]>
- mx:Script>
- mx:Application>