Flash Remoting For .Net 的跨域连接问题解决~


    关于Flash Remoting 跨域问题,今天折腾了半天,终于有了进展。

    假设Remoting Service在DomainA.com上,而一个swf发布在DomainB.com上,现在DomainB.com上的swf要连接DomainA的Remoting Service,现在就是跨域的Remoting连接了。

    Flash Remoting与.Net的Remoting不同,.Net可以使用TCP或HTTP协议的传输信道,而Flash Remoting只能使用HTTP协议的传输信道,在Flash Player 5或更早版本的播放器中,跨域或子域访问不受限制,在Flash Player 7播放器中,播放器执行精确域匹配而不是超域匹配规则,即在未经允许的情形下,跨域访问是禁止的。

    在Luar站上说,“SWF文件在domainA.com,Flash Remoting Gateway在domainB.com”,domainA中的swf连接domainB的Remoting Gateway,“測試過,這個當然是不可以的”。
    看到这句时,心里着实凉了大半截~问了几个群,半下午,没有解决,后来实在没办法了问Peter,Peter说“跨域需要另外一个域的服务器添加配置文件”,再查帮助~哈哈~,果然可以通过域策略文件解决。

    Flash Remoting要实现跨域连接,需要创建一个“crossdomain.xml”的域策略文件,名称必须为“crossdomain.xml”,然后放置在DomainA(即Remoting Service服务器)的站点根目录下。
    文件内容是一个允许连接的Client端域名或IP列表,应该如:

<?xml version="1.0"?>
<cross-domain-policy>
  
<allow-access-from domain="192.168.39.209" />
  
<allow-access-from domain="*.DomainB.com" />
</cross-domain-policy>

    放置后,crossdomain.xml文件应该可以这样访问:http://domainA.com/crossdomain.xml

    好了,这样做就可以跨域连接Remoting Service了。


    下边是具体测试代码:
    1.Client端
    Client端的Swf发布在192.168.39.209。

    Swf 文件:
    场景中放置了一个txt的动态文本框,然后在第一帧加入如下代码(AS2.0写法):

import mx.remoting.Service;
import mx.rpc.RelayResponder;
import mx.remoting.PendingCall;
import mx.rpc.ResultEvent;
import mx.rpc.FaultEvent;
/*import mx.remoting.debug.NetDebug;
NetDebug.initialize();
*/

import mx.services.Log;
function sayGreeting_Result(evt:ResultEvent):Void {
 txt.text 
= evt.result;
}

function sayGreeting_Fault(evt:FaultEvent):Void {
 
//trace("Error: "+evt.fault.__faultstring);
 txt.text = evt.fault.faultstring;

}

var gatewayPath = "http://192.168.39.109/MyRemoting/gateway.aspx";
var myResponder = new RelayResponder(this"sayGreeting_Result""sayGreeting_Fault");
//var service:Service = new Service(gatewayPath, new Log(), "MyRemoting", null, myResponder);
var service:Service = new Service(gatewayPath, null"MyRemoting"null, myResponder);
var mypc:PendingCall = service.test("Yao.NET");

    2.Server端
    Remoting Service发布在192.168.39.109,IIS中创建MyRemoting虚拟目录,此目录下有gateway.aspx,web.config,test.aspx文件及Bin目录,其中Gateway.aspx,web.config文件直接从flashremoting的samples下复制过来即可。

    test.aspx文件:

<%@ Page language="c#" debug="true" %>
<%@ Register TagPrefix="Macromedia" Namespace="FlashGateway" Assembly="flashgateway" %>
<Macromedia:Flash ID="Flash" Runat="Server" />
<%
        String message 
= "你好呀:";
        
if (Flash.Params.Count > 0)
        
{
            message 
+= Flash.Params[0].ToString();
        }


        Flash.Result 
= message;
%>

    web.config中配置:

<system.web>
<httpModules>
        
<add name="GatewayController" type="FlashGateway.Controller.GatewayController,flashgateway" />
    
</httpModules>
</system.web>


    Bin目录:
    中有flashgateway.dll,frconfig.txt,wsdl.exe(此实例中非必须)文件,前两个文件也是直接从flashremoting的samples下复制过来即可。

    ---------------------------------------
    以上代码,swf在Flash8中编写,test.aspx在vs.net 2003中编写,均测试通过。

    最后在此感谢Peter~

posted on 2006-06-14 18:46  三千  阅读(4024)  评论(5编辑  收藏  举报