Nicolas Cannasse 18 年之前
父节点
当前提交
baa5ed4b56
共有 2 个文件被更改,包括 69 次插入0 次删除
  1. 4 0
      std/flash/system/Security.hx
  2. 65 0
      std/haxe/remoting/FlashJsConnection.hx

+ 4 - 0
std/flash/system/Security.hx

@@ -6,6 +6,10 @@ extern class Security
 	static function allowInsecureDomain( domain : String ):Void;
 	static function allowInsecureDomain( domain : String ):Void;
 	static function loadPolicyFile(url:String):Void;
 	static function loadPolicyFile(url:String):Void;
 
 
+	#if flash8
+	static function sandboxType(default,null) : String;
+	#end
+
 	private static function __init__() : Void untyped {
 	private static function __init__() : Void untyped {
 		flash.system.Security = _global["System"]["security"];
 		flash.system.Security = _global["System"]["security"];
 	}
 	}

+ 65 - 0
std/haxe/remoting/FlashJsConnection.hx

@@ -0,0 +1,65 @@
+package haxe.remoting;
+
+class FlashJsConnection extends haxe.remoting.AsyncConnection {
+
+	#if flash
+
+	override function __resolve( field : String ) : AsyncConnection {
+		var c = new FlashJsConnection(__data,__path.copy());
+		c.__error = __error;
+		c.__path.push(field);
+		return c;
+	}
+
+	override public function call( params, ?onData ) {
+		var p = __path.copy();
+		var f = p.pop();
+		var path = p.join(".");
+		var s = new haxe.Serializer();
+		s.serialize(params);
+		var cnx : { private function escapeString(s : String) : String; } = haxe.remoting.Connection;
+		var params = cnx.escapeString(s.toString());
+		var me = this;
+		haxe.Timer.delayed(function() {
+			var s = flash.external.ExternalInterface.call("haxe.remoting.FlashJsConnection.flashCall",me.__data,path,f,params);
+			var v = null;
+			try {
+				if( s == null )
+					throw "Failed to call JS method "+path;
+				v = { r : new haxe.Unserializer(s).unserialize() };
+			} catch( e : Dynamic ) {
+				me.onError(e);
+			}
+			if( v != null )
+				onData(v.r);
+		},0)();
+	}
+
+	public static function flashConnect( objId : String ) : AsyncConnection {
+		if( !flash.external.ExternalInterface.available )
+			throw "External Interface not available";
+		if( flash.external.ExternalInterface.call("haxe.remoting.FlashJsConnection"+".jsRemoting") != "yes" )
+			throw "haxe.remoting.FlashJsConnection"+" is not available in JavaScript";
+		return new FlashJsConnection(objId,[]);
+	}
+
+	#else js
+
+	static function jsRemoting() {
+		return "yes";
+	}
+
+	static function flashCall( flashObj : String, path : String, f : String, params : String ) : String {
+		try {
+			var cnx : { private var __data : Dynamic; } = haxe.remoting.Connection.flashConnect(flashObj);
+			return cnx.__data.remotingCall(path,f,params);
+		} catch( e : Dynamic ) {
+			var s = new haxe.Serializer();
+			s.serializeException(e);
+			return s.toString();
+		}
+	}
+
+	#end
+
+}