FluorineFx:远程共享对象(Remote SharedObjects)

单纯从客户端上来看,FluorineFx的RSO跟FMS中的RSO几乎没什么不同(参见Flash/Flex学习笔记(15):FMS 3.5之远程共享对象(Remote Shared Object) ),只不过FMS是Adobe的收费产品,FluorineFx是用于.Net平台的开源免费产品

.

服务端代码:

1、为了防止客户端随意连接或创建任何属性的RSO,服务端可以定义一个用于安全处理的cs文件

using System.Collections;
using FluorineFx.Messaging.Api;
using FluorineFx.Messaging.Api.SO;

namespace _02_SharedObject
{
    public class SampleSOSecurityHandler : ISharedObjectSecurity 
    {
        #region ISharedObjectSecurity Members

        //是否允许连接
        public bool IsConnectionAllowed(ISharedObject so)
        {            
            return true;
        }

        //是否允许创建rso对象(下面的代码仅允许创建名为position的rso对象)
        public bool IsCreationAllowed(IScope scope, string name, bool persistent)
        {
            if (!"position".Equals(name) || !persistent)
                return false;
            else
                return true;
        }

        //是否允许删除
        public bool IsDeleteAllowed(ISharedObject so, string key)
        {
            return false;
        }

        //是否允许rso对象向服务端send指定回调方法(下面的代码仅允许发送名为drop的回调方法)
        public bool IsSendAllowed(ISharedObject so, string message, IList arguments)
        {
            if (!"drop".Equals(message))
            {
                return false;
            }
            else
            {
                //这里把第一个参数处理一下,客户端可以在drop()方法中访问到处理后的参数
                if (arguments.Count > 0) {
                    arguments[0] = "from server :" + arguments[0];
                }
                return true;
            }
        }

        //rso对象的属性是否可写
        public bool IsWriteAllowed(ISharedObject so, string key, object value)
        {
            return true;
        }

        #endregion
    }
}

2、创建一个ApplicationAdapter(应用适配器)

using FluorineFx.Messaging.Adapter;
using FluorineFx.Messaging.Api;
using FluorineFx.Messaging.Api.SO;


namespace _02_SharedObject
{
    /// <summary>
    /// Summary description for AppHandler.
    /// </summary>
    public class SharedBallApplication : ApplicationAdapter
    {
        public SharedBallApplication()
        {
        }

        public override bool AppStart(IScope application)
        {
            RegisterSharedObjectSecurity(new SampleSOSecurityHandler());//注册刚才定义的安全处理Handler
            return base.AppStart(application);
        }

        /// <summary>
        /// 每当有新客户端连接到服务器时,该方法会被触发
        /// </summary>
        /// <param name="connection"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public override bool AppConnect(IConnection connection, object[] parameters)
        {
            string userName=string.Empty, passWord=string.Empty;

            if (parameters.Length >= 2)
            {
                userName = parameters[0].ToString();//第一个参数当作用户名
                passWord = parameters[1].ToString();//第二个参数当作密码
            }

            if (userName == "jimmy.yang" && passWord == "123456")//安全性校验
            {
                if (base.AppConnect(connection, parameters))
                {
                    //获取共享对象(position)
                    ISharedObject users_so = GetSharedObject(connection.Scope, "position");
                    if (users_so == null)
                    {
                        //创建共享对象
                        CreateSharedObject(connection.Scope, "position", true);
                        users_so = GetSharedObject(connection.Scope, "position");
                    }

                    //更新共享对象
                    users_so.SetAttribute("x", 200);
                    users_so.SetAttribute("y", 200);


                    connection.Client.SetAttribute("clientName", userName); //回传数据到客户端连接

                    return true;
                }
                else {
                    RejectClient("连接失败,请检查服务端是否运行正常!");
                    return false;
                }
            }
            else 
            {
                RejectClient("用户名或密码错误");
                return false;
            }          

             
        }
       
    }
}

最后还要一些配置文件来提供必要的信息,详情见最后的示例源码下载

Flash客户端代码:(几乎跟在FMS环境中完全一样)

import flash.display.StageAlign;
import flash.net.NetConnection;
import flash.events.NetStatusEvent;
import flash.net.SharedObject;
import flash.events.SyncEvent;
import flash.events.MouseEvent;

stage.align = StageAlign.TOP_LEFT;

var nc:NetConnection = new NetConnection();
var ball_so:SharedObject;
var userName:String = "";
var passWord:String = "";
var remoteUrl:String = "";

if (root.loaderInfo.parameters.remotingGatewayPath != null)
{
	remoteUrl = root.loaderInfo.parameters.remotingGatewayPath;
}
else
{
	remoteUrl = "rtmp://localhost:1935/SharedBall";
}

if (root.loaderInfo.parameters.userName != null)
{
	userName = root.loaderInfo.parameters.userName;
}
else
{
	userName = "jimmy.yang";
}

if (root.loaderInfo.parameters.passWord != null)
{
	passWord = root.loaderInfo.parameters.passWord;
}
else
{
	passWord = "123456";
}

this.nc.addEventListener(NetStatusEvent.NET_STATUS,net_status_handler);
this.nc.connect(remoteUrl,userName,passWord);
this.nc.client = this;
this.txtStatus.text = "服务端连接中...";
SharedBall_mc.buttonMode = true;

function net_status_handler(e:NetStatusEvent):void
{
	this.txtStatus.text = "as3 状态:" + e.info.code;
	if (e.info.code == "NetConnection.Connect.Success")
	{
		ball_so = SharedObject.getRemote("position",nc.uri,true);
		this.ball_so.addEventListener(SyncEvent.SYNC,sync_handler);
		this.ball_so.connect(this.nc);
		this.ball_so.client = this;
	}
}


function sync_handler(e:SyncEvent):void
{
	if (ball_so.data.x == undefined || ball_so.data.y == undefined)
	{
		SharedBall_mc.x = 100;
		SharedBall_mc.y = 100;
	}
	else
	{
		SharedBall_mc.x = ball_so.data.x;
		SharedBall_mc.y = ball_so.data.y;
	}
}

SharedBall_mc.addEventListener(MouseEvent.MOUSE_DOWN,mouse_down_handler);

function mouse_down_handler(e:MouseEvent):void
{
	SharedBall_mc.startDrag();
	this.stage.addEventListener(MouseEvent.MOUSE_UP,mouse_up_handler);
	this.stage.addEventListener(MouseEvent.MOUSE_MOVE,mouse_move_handler);
}

function mouse_move_handler(e:MouseEvent):void
{
	ball_so.setProperty("x",this.mouseX);
	ball_so.setProperty("y",this.mouseY);
	trace(ball_so.data.x,ball_so.data.y);
}

function mouse_up_handler(e:MouseEvent):void
{
	SharedBall_mc.stopDrag();
	this.stage.removeEventListener(MouseEvent.MOUSE_UP,mouse_up_handler);
	this.stage.removeEventListener(MouseEvent.MOUSE_MOVE,mouse_move_handler);
	ball_so.send("drop",this.userName);
}


function drop(msg:Object):void{
	this.txtStatus.text = msg.toString();
}

 运行截图:

在这个示例中,小球的坐标来自于服务端的共享对象属性值,所以如果一切顺利,在不同的机器上浏览“嵌入有该swf的页面”,当一台机器上拖动小球时,另一台机器上的小球也会随之运动。

示例源代码下载:http://cid-2959920b8267aaca.office.live.com/self.aspx/Flash/FluorineFx^_02.rar

 

posted @ 2010-08-26 19:13  菩提树下的杨过  阅读(1152)  评论(0编辑  收藏  举报