原来就是flex 和C#通讯的框架fluorinefx
flex 和java通讯的框架blazdes 这两个框架直接交互。把flex那块抛弃
c#通过fluorinefx调用java,java通过blazeds来调用c#
光写了C#调用java,java调用C# 还没写,瞌睡的不行了!
好处就是AMF貌似快,比xml传输更简单!
using System; using FluorineFx; using FluorineFx.Net; using FluorineFx.Messaging.Api; using FluorineFx.Messaging.Api.Service; using FluorineFx.AMF3; namespace AMFCall { class Program { NetConnection _netConnection; static void Main(string[] args) { Program program = new Program(); //program.ConnectStr();//传递字符串,返回字符串 //program.ConnectgetObj();//返回java对象 program.Connect();//传递C#对象到java端,并返回java对象 System.Console.ReadLine(); } public void ConnectStr() { // Create NetConnection client _netConnection = new NetConnection(); _netConnection.ObjectEncoding = ObjectEncoding.AMF3; _netConnection.NetStatus += new NetStatusHandler(_netConnection_NetStatus); //_netConnection.Connect("http://localhost:2896/WebSite/Gateway.aspx"); _netConnection.Connect("http://localhost:8080/blazeds/messagebroker/amf"); System.Console.WriteLine("*** Flash RPC ***"); // _netConnection.Call("ServiceLibrary.MyDataService.GetCustomers", new GetCustomersHandler(), new object[] { "415" }); System.Console.WriteLine("*** Flex RPC ***"); //_netConnection.Call("my-amf", "fluorine", "ServiceLibrary.MyDataService", "GetCustomers", new GetCustomersHandler(), new object[] { "415" }); _netConnection.Call("my-amf", "net", "net.Hello", "sayHello", new RemoteHandler(), new Object[] { "415" }); System.Console.WriteLine("Press 'Enter' to exit"); } public void ConnectgetObj() { // Create NetConnection client _netConnection = new NetConnection(); _netConnection.ObjectEncoding = ObjectEncoding.AMF3; _netConnection.NetStatus += new NetStatusHandler(_netConnection_NetStatus); //_netConnection.Connect("http://localhost:2896/WebSite/Gateway.aspx"); _netConnection.Connect("http://localhost:8080/blazeds/messagebroker/amf"); System.Console.WriteLine("*** Flash RPC ***"); // _netConnection.Call("ServiceLibrary.MyDataService.GetCustomers", new GetCustomersHandler(), new object[] { "415" }); System.Console.WriteLine("*** Flex RPC ***"); //_netConnection.Call("my-amf", "fluorine", "ServiceLibrary.MyDataService", "GetCustomers", new GetCustomersHandler(), new object[] { "415" }); _netConnection.Call("my-amf", "net", "net.Hello", "sayHelloObj", new RemoteHandler(), new Object[] { "415" }); System.Console.WriteLine("Press 'Enter' to exit"); } public void Connect() { // Create NetConnection client _netConnection = new NetConnection(); _netConnection.ObjectEncoding = ObjectEncoding.AMF3; _netConnection.NetStatus += new NetStatusHandler(_netConnection_NetStatus); //_netConnection.Connect("http://localhost:2896/WebSite/Gateway.aspx"); _netConnection.Connect("http://localhost:8080/blazeds/messagebroker/amf"); System.Console.WriteLine("*** Flash RPC ***"); // _netConnection.Call("ServiceLibrary.MyDataService.GetCustomers", new GetCustomersHandler(), new object[] { "415" }); System.Console.WriteLine("*** Flex RPC ***"); net.NetJavaObj nj = new net.NetJavaObj(); nj.id = 88; nj.name = "sdfafa"; //_netConnection.Call("my-amf", "fluorine", "ServiceLibrary.MyDataService", "GetCustomers", new GetCustomersHandler(), new object[] { "415" }); _netConnection.Call("my-amf", "net", "net.Hello", "sayHelloNet", new RemoteHandler(), new Object[] { nj}); System.Console.WriteLine("Press 'Enter' to exit"); } void _netConnection_NetStatus(object sender, NetStatusEventArgs e) { string level = e.Info["level"] as string; if (level == "error") { //received an error System.Console.WriteLine("Error: " + e.Info["code"] as string); } if (level == "status") { System.Console.WriteLine("Status: " + e.Info["code"] as string); } } } public class RemoteHandler : IPendingServiceCallback { public void ResultReceived(IPendingServiceCall call) { object result = call.Result; Type type = call.Result.GetType(); Console.WriteLine(type.Name); Console.WriteLine("result" + result); //NetJavaObj nj = (NetJavaObj)result; //Console.WriteLine("i" + nj.id + "//name:" + nj.name); //Console.ReadLine(); } } public class GetCustomersHandler : IPendingServiceCallback { public void ResultReceived(IPendingServiceCall call) { object result = call.Result; System.Console.WriteLine("Server response: " + result); //DataAccess sample sends back an ArrayCollection (AMF3) ArrayCollection items = result as ArrayCollection; foreach (object item in items) { Flex.CustomerVO customer = item as Flex.CustomerVO; System.Console.WriteLine(customer.firstname + " " + customer.lastname); } } } }
using System;
using System.Collections.Generic; using System.Text; namespace net { class NetJavaObj { public int id; public String name; } }
package net;
import flex.messaging.io.ClassAliasRegistry; public class Hello { public String sayHello(String name){ return name+"afan"; } public NetJavaObj sayHelloObj(String name){ return new NetJavaObj(1,name); } public NetJavaObj sayHelloNet(NetJavaObj obj){ System.out.println(obj.getId()+":"+obj.getName()); obj.setId(323); obj.setName("afan"); return obj; } /** * @param args */ public static void main(String[] args) { } }
package net;
import flex.messaging.io.ClassAliasRegistry; public class NetJavaObj { private int id; private String name; public NetJavaObj(int id, String name) { super(); this.id = id; this.name = name; } public NetJavaObj() { super(); this.id = id; this.name = name; } /** * @param args */ public static void main(String[] args) { } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
blazeds配置文件remoting-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<service id="remoting-service" class="flex.messaging.services.RemotingService"> <adapters> <!-- <adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/> --> <adapter-definition id="java-object" class="test.Adapter" default="true"/> </adapters> <default-channels> <channel ref="my-amf"/> </default-channels> <destination id="net"> <properties> <source>net.Hello</source> </properties> </destination> </service>
test.Adapter
package test;
import flex.messaging.io.ClassAliasRegistry; import flex.messaging.messages.Message; import flex.messaging.services.remoting.adapters.JavaAdapter; public class Adapter extends JavaAdapter { @Override public Object invoke(Message message) { System.out.println("invoke"); Object obj=null; try{ //不太清楚是调用之前注册还是调用之后注册,注册后C#就直接转成相应的对象, //而不是ASObject ClassAliasRegistry cr=ClassAliasRegistry.getRegistry(); cr.registerAlias("net.NetJavaObj", "net.NetJavaObj"); obj=super.invoke(message); cr.registerAlias("net.NetJavaObj", "net.NetJavaObj"); }catch(Exception e){ e.printStackTrace(); } return obj; } }