一、添加一个新的FluorineFx的服务类项目OnLineService,删除原有的Sample.cs,并添加一个用户类定义与一个ApplicationAdpater类:如下:
/*-- User.cs --*/
namespace OnLineService
{
public class User
{
public string UserName { get; set; }
public string UserPsw { get; set; }
}
}
/* -- ApplicationAdapter类:DemoApp.cs --*/
using FluorineFx;
using FluorineFx.Messaging.Adapter;
using FluorineFx.Messaging.Api;
using FluorineFx.Messaging.Api.SO;
namespace OnLineService
{
/// <summary>
/// Fluorine sample service.
/// </summary>
[RemotingService("用户登录状态测试程序")]
public class DemoApp:ApplicationAdapter
{
public override bool AppConnect(IConnection connection, object[] parameters)
{
string userName = parameters[0] as string;
string password = parameters[1] as string;
// 这里可以自定义登录判断验证逻辑,此处只要密码为000即可进入
if (password != "000")
return false;
// 将当前连接添加一个userName属性与当前登录用户关联起来
// 以便此连接掉线时,可以准确定位到其相关用户
connection.Client.SetAttribute("userName", userName);
//获取共享对象(OnLineUsers)
ISharedObject users_so = GetSharedObject(connection.Scope, "OnLineUsers");
if (users_so == null)
{
//第一个用户时共享对象不存在,需创建
CreateSharedObject(connection.Scope, "OnLineUsers", false);
users_so = GetSharedObject(connection.Scope, "OnLineUsers");
}
//更新共享对象key为用户名,value为用户对象
//value可以为更复杂的对象,但尽量不要打包List或array,因为Flex端解包不易
users_so.SetAttribute(userName, new User() { UserName = userName, UserPsw = password });
return true;
}
public override void AppDisconnect(IConnection connection)
{
string userName = connection.Client.GetAttribute("userName") as string;
ISharedObject users_so = GetSharedObject(connection.Scope, "OnLineUsers");
if (users_so != null)
{
//从共享对象中移除当前退出系统用户
users_so.RemoveAttribute(userName);
}
base.AppDisconnect(connection);
}
}
}
二、在解决方案中添加一个Fluorine.net Web项目
2.1 在Web根目录下添加apps文件夹,apps下添加OnLineUser文件夹,OnLineUser文件夹下添加app.config配置文件,其下配置应用程序处理为上面定义的DemoApp,如下:
2.2 修改WEB-INF下的service-config.xml,打开其my-rtmp信道配置节(fluorineFx添加的web项目时,默认会将此节注释掉,打开即可)
2.3 在根目录下添加Flex文件下,其下加入libs、src、swf三个子文件夹,将fds.swc拷入libs文件夹,其它两个文件夹在后面的步骤中分别用作 Flex项目的源文件与输出文件目录。
项目的文件组织如下:
三、在Flex Builder3中新建项目OnLineUserDemo,项目路径为2.3中的Flex文件夹,配置测试服务器为asp.net,WebRootUrl路径为vs运行网站时的路径,如下图中的兰色部分:
3.1 右击flex项目的属性(Properties),
配置FlexBuilderPath如下:
配置FlexCompier的参数如下:
注:上面的“E:..”请换成你本地项目中services-config.xml对应的路径(包括下面的Server配置)
配置FlexServce的参数如下:
3.2 以下为OnLineUserDemo.mxml源代码:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
width="600" height="382" backgroundGradientAlphas="[1.0, 1.0]"
backgroundGradientColors="[#000000, #049FF1]" fontSize="12">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.messaging.config.ServerConfig;
private var nc:NetConnection;
private var so:SharedObject;
private function connectionServer(event:MouseEvent):void
{
var endpoint:String=ServerConfig.getChannel("my-rtmp").endpoint;
nc = new NetConnection();
nc.connect(endpoint+"/OnLineUser",this.txtUserName.text,this.txtPassword.text);
nc.addEventListener(NetStatusEvent.NET_STATUS,onStatusHandler);
nc.client = this;
}
private function onStatusHandler(event:NetStatusEvent):void
{
this.connStatus.text = "连接状态:" + event.info.code;
if(event.info.code == "NetConnection.Connect.Success")
{
//连接远程共享对象
so = SharedObject.getRemote("OnLineUsers",nc.uri,false);
if(so)
{
so.addEventListener(SyncEvent.SYNC,onSyncHandler);
so.connect(nc);
so.client = this;
}
onCallClient("用户【 <font color=\"#FF0000\">"+this.txtUserName.text+"</font>】登陆了系统!");
}
}
private function onSyncHandler(event:SyncEvent):void
{
var temp:Array = new Array();
//so.data中每一个元素即服务器端维护的每一个attribute
for(var u:Object in so.data)
{
temp.push(so.data[u]);
}
this.dgUserList.dataProvider = temp;
}
private function onCallClient(message:String):void
{
so.send("writeMsg",message);
//所以通过so的send方法,可以同时调用每个客户端的writeMsg程序,类似发送广播
}
//writeMsg一定要为public,否则so调用不到
public function writeMsg(message:Object):void
{
var param:String=message.toString();
txtTraceArea.htmlText += param + "\n";
txtTraceArea.validateNow();
txtTraceArea.verticalScrollPosition = txtTraceArea.maxVerticalScrollPosition;
}
]]>
</mx:Script>
<mx:Label x="302" y="153" id="connStatus" width="288" color="#FFFFFF"/>
<mx:Label x="302" y="181" id="nowRoom" width="288" color="#FFFFFF" />
<mx:DataGrid x="24" y="30" height="332" width="248" id="dgUserList">
<mx:columns>
<mx:DataGridColumn dataField="UserName" headerText="姓名"/>
<mx:DataGridColumn dataField="UserPsw" headerText="蜜码"/>
</mx:columns>
</mx:DataGrid>
<mx:Form x="302" y="30" width="236">
<mx:FormItem label="用户名:" color="#FFFFFF">
<mx:TextInput id="txtUserName" width="130" color="#000000"/>
</mx:FormItem>
<mx:FormItem label="密 码:" color="#FFFFFF">
<mx:TextInput id="txtPassword" width="130"
color="#000000" displayAsPassword="true"/>
</mx:FormItem>
<mx:FormItem label="">
<mx:Button label="登陆服务器" click="connectionServer(event)"
enabled="{this.txtUserName.text.length>0?true:false}" color="#FFFFFF"/>
</mx:FormItem>
</mx:Form>
<mx:TextArea x="302" y="209" width="288" height="153" alpha="1.0"
backgroundColor="#F2D2D2" backgroundAlpha="0.26" color="#FFFFFF"
id="txtTraceArea" borderColor="#FFFFFF"/>
</mx:Application>
效果图: