一般在使用Flash Remoting功能的時候
都是安裝Remoting Library AS1、AS2 Component來用
其實是可以直接用NetConnection呼叫Remoting的
因為AMF在Flash Player中的Serialize、Deserialize動作
都是透過NetConnection於底層完成,並非寫在AS
就是因為寫在底層,所以AMF會比其他格式來得更有效率
NetConnection具有兩種連線方式,決定於要連線URL通訊協定
對FlashCom RTMP連線是像Socket持續性即時連線
對Remoting Server HTTP則是非持續性連線
不過兩者都是以AMF格式作序列化
以下示範用NetConnection呼叫Remoting方式:
ActionScript Code:
- var nc:NetConnection = new NetConnection();
- var url:String = "http://192.168.0.10:8084/FlashRemoting/gateway";
- nc.onResult = function(data) {
- trace("onResult : "+data);
- };
- nc.onStatus = function(info) {
- trace("onStatus : "+info);
- for (var i in info) {
- trace("info["+i+"] : "+info[i]);
- }
- };
- nc.connect(url);
- nc.call("swl.NewClass.Test", nc, 11);
- //output: onResult : Test() with Number : 11.0
Java Class Code:
- package swl;
- public class NewClass {
- public NewClass() {
- }
- public java.util.Map Test(java.util.Map map) {
- return map;
- }
- public String Test(java.util.ArrayList arraylist) {
- return "Test() with ArrayList : " + arraylist;
- }
- public String Test(String str) {
- return "Test() with String : " + str;
- }
- public String Test(String[] str) {
- return "Test() with String Array : " + str;
- }
- public String Test(Number num) {
- return "Test() with Number : " + num;
- }
- public String Test(boolean bo) {
- return "Test() with boolean : " + bo;
- }
- public String Test() {
- return "Test() no arguments";
- }
- }
此文收藏自Ticore‘s Blog: http://ticore.blogspot.com/2005/09/remoting-by-netconnection.html